2013-02-04 20 views
0

我有兩個班。首先通過使用IsolatedStorage從ToggleSwitchButton使用存儲布爾值。如何從其他類中的IsolatedStorage中檢索值?

就像這個...

private void tglSwitch_Checked(object sender, RoutedEventArgs e) 
    { 
     System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["EnableLocation"] = true; 
    } 
    private void tglSwitch_Unchecked(object sender, RoutedEventArgs e) 
    { 
     System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["EnableLocation"] = false; 
    } 

第二類將使用布爾值從第一類做一些事情。

就像這個...

 if(booleanValFromFirst){ 
     //Do something 
    } 
    else{ 
     //Do something 
    } 

感謝。

回答

2

這是,你想要什麼?

if ((bool)System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["EnableLocation"] == true) 

P.S.我建議您爲所有值創建一個類,存儲在應用程序設置中並使用它。

像這樣:

public static class SettingsManager 
    { 
     private static IsolatedStorageSettings appSettings; 

     public static IsolatedStorageSettings AppSettings 
     { 
      get { return SettingsManager.appSettings; } 
      set { SettingsManager.appSettings = value; } 
     } 

     public static void LoadSettings() 
     { 
      // Constructor 
      if (appSettings == null) 
       appSettings = IsolatedStorageSettings.ApplicationSettings; 

      // Generate Keys if not created 
      if (!appSettings.Contains(Constants.SomeKey)) 
       appSettings[Constants.SomeKey] = "Some Default value"; 

      // generate other keys    

     } 
    } 

然後你就可以用類實例工作

在您啓動類初始化它爲SettingsManager.LoadSettings();

一個在任意一個類只需要調用它:

if ((bool)SettingsManager.AppSettings[Constants.SomeBoolKey]) 
    doSomething(); 
+0

謝謝你的回答,明天我會試試。 :) –

相關問題