2013-10-30 45 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个变量,它可以在应用程序启动时定义,隔离存储只是矫枉过正。

相关问题