在Windows手机中存储应用程序常见数据的位置?

时间:2013-10-30 09:29:24

标签: windows-phone-7

我的所有10个页面都有一个变量,我应该在哪里存储它以便所有页面都可以访问它?通过将变量保存在APPDELEGATE中,可以在iOS中完成相同的任务。 Windows Phone中的解决方案是什么?

2 个答案:

答案 0 :(得分:0)

你应该看看some background reading来帮助

隔离存储设置的示例代码 希望这会对你有所帮助

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;
        }
    }

答案 1 :(得分:0)

根据你的评论,如果你有一个公共财产,如:

public string MyStaticVariable { get; set; }
MyStaticVariable = "SomeValue";
在App.xaml.cs中定义的

,您可以通过以下方式在每个页面中访问它:

App.MyStaticVariable;

我的观点:如果你在谈论可以在你的应用启动时定义的1个变量,那么隔离存储就是矫枉过正。