2011-11-13 18 views
1

簡單的問題,之前問過,但沒有人給我正確的答案,所以我要非常具體。從另一個頁面讀取IsolatedStorage中的設置

我需要在Windows Phone中讀取IsolatedStorage的兩個設置。設置爲ExitAlertOrientationLock。我已經設置了設置,它們保存得很好,我只需要知道如何從另一頁面讀取它們。變量設置的頁面是Settings.cs,我需要從MainPage.xaml.cs中讀取設置。我也需要知道如何只做一些事情,如果變量是真或假。我想我應該使用if-then-else語句,但我只是想仔細檢查。

我的應用程序是用C#編寫的。這是我使用的存儲設置代碼:

using System; 
using System.IO.IsolatedStorage; 
using System.Diagnostics; 

namespace Google_ 
{ 
    public class AppSettings 
    { 

     IsolatedStorageSettings isolatedStore; 

     // isolated storage key names 
     const string ExitWarningKeyName = "ExitWarning"; 
     const string OrientationLockKeyName = "OrientationLock"; 

     // default values 
     const bool ExitWarningDefault = true; 
     const bool OrientationLockDefault = false; 

     public AppSettings() 
     { 
      try 
      { 
       // Get the settings for this application. 
       isolatedStore = IsolatedStorageSettings.ApplicationSettings; 

      } 
      catch (Exception e) 
      { 
       Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString()); 
      } 
     } 


     public bool AddOrUpdateValue(string Key, Object value) 
     { 
      bool valueChanged = false; 

      // If the key exists 
      if (isolatedStore.Contains(Key)) 
      { 
       // If the value has changed 
       if (isolatedStore[Key] != value) 
       { 
        // Store the new value 
        isolatedStore[Key] = value; 
        valueChanged = true; 
       } 
      } 
      // Otherwise create the key. 
      else 
      { 
       isolatedStore.Add(Key, value); 
       valueChanged = true; 
      } 

      return valueChanged; 
     } 

     public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue) 
     { 
      valueType value; 

      // If the key exists, retrieve the value. 
      if (isolatedStore.Contains(Key)) 
      { 
       value = (valueType)isolatedStore[Key]; 
      } 
      // Otherwise, use the default value. 
      else 
      { 
       value = defaultValue; 
      } 

      return value; 
     } 

     public void Save() 
     { 
      isolatedStore.Save(); 
     } 

     public bool ExitWarning 
     { 
      get 
      { 
       return GetValueOrDefault<bool>(ExitWarningKeyName, ExitWarningDefault); 
      } 
      set 
      { 
       AddOrUpdateValue(ExitWarningKeyName, value); 
       Save(); 
      } 
     } 

     public bool OrientationLock 
     { 
      get 
      { 
       return GetValueOrDefault<bool>(ExitWarningKeyName, OrientationLockDefault); 
      } 
      set 
      { 
       AddOrUpdateValue(OrientationLockKeyName, value); 
       Save(); 
      } 
     } 

    } 
} 

如果有不清楚的地方,只是讓我知道這樣我就可以進一步解釋。謝謝。

回答

3

我做的是以下內容添加到我的AppSettings類:

private static AppSettings _default = new AppSettings(); 
/// <summary> 
/// Gets the default instance of the settings 
/// </summary> 
public static AppSettings Default 
{ 
    get 
    { 
     return _default; 
    } 
} 

然後你就可以在你的項目中只使用AppSettings.Default從任何地方:

if (AppSettings.Default.ExitWarning) 
{ 
} 
else 
{ 
} 

讓我知道你是否需要更多信息。

+0

我不太明白這是如何工作。我會只用'if(AppSettings.ExitAlert == true)'來獲得一個特定的布爾值...? – JacobTheDev

+0

我已經用示例更新了我的答案 – calum

+0

因此,應該從任何頁面開始工作?編輯:啊!它完美的工作!非常感謝! – JacobTheDev

1

您還可以使用PhoneApplicationService.Current.State[]來存儲應用程序範圍的設置。他們可以從任何頁面訪問,而且您不需要隔離存儲來存儲它們。

例子:

PhoneApplicationService.Current.State["ExitWarning"] = true; 
相關問題