2013-10-30 46 views
0

我有一個變量用在我所有的10頁中,我應該在哪裏存儲它以便它可以被所有頁面訪問?通過將變量保存在APPDELEGATE中,可以在iOS中完成相同的任務。 Windows Phone中的解決方案是什麼?在windows phone中存儲應用程序通用數據的位置?

+2

您正在使用哪種語言?在C#中,您可以使用App.cs或隔離存儲器 –

+0

好的!如果我在App.xaml.cs中保存一個變量,那麼我怎樣才能在頁面1中引用它? – Aju

回答

0

你應該看看some background reading,以幫助IsolatedStorageSettings

示例代碼 希望這將幫助你

public class AppSettings 
    { 
     // Our settings 
     IsolatedStorageSettings settings; 

     // The key names of our settings 
     const List<String> PropertyIdList   = null; 
     const List<String> FavPropertyIdList  = null; 
     const string SearchSource     = null; 
     const string[] Suggestions     = null; 
     const string PropertyId      = null; 
     const string AgentContactInfo    = null; 
     const string AgentShowPhoto     = null; 

     /// <summary> 
     /// Constructor that gets the application settings. 
     /// </summary> 
     public AppSettings() 
     { 
      // Get the settings for this application. 
      settings = IsolatedStorageSettings.ApplicationSettings; 
     } 

     /// <summary> 
     /// Update a setting value for our application. If the setting does not 
     /// exist, then add the setting. 
     /// </summary> 
     /// <param name="Key"></param> 
     /// <param name="value"></param> 
     /// <returns></returns> 
     public bool AddOrUpdateValue(string Key, Object value) 
     { 
      bool valueChanged = false; 

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

     /// <summary> 
     /// Get the current value of the setting, or if it is not found, set the 
     /// setting to the default setting. 
     /// </summary> 
     /// <typeparam name="T"></typeparam> 
     /// <param name="Key"></param> 
     /// <param name="defaultValue"></param> 
     /// <returns></returns> 
     public T GetValueOrDefault<T>(string Key, T defaultValue) 
     { 
      T value; 

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

根據您的意見,如果你有一個公共屬性,如:

public string MyStaticVariable { get; set; } 
MyStaticVariable = "SomeValue"; 

在您的App.xaml.cs中定義,您可以通過以下方式訪問它:

App.MyStaticVariable; 

我的意見:如果你正在談論1個變量,它可以在應用程序啓動時定義,隔離存儲只是矯枉過正。

相關問題