0

我有一个变量在我的所有 10 个页面中都使用,我应该将它存储在哪里以便所有页面都可以访问它?同样的任务可以在 iOS 中通过将变量保存在APPDELEGATE. windows phone 中的解决方案是什么?

4

2 回答 2

0

你应该看看一些背景阅读来帮助

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

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;
        }
    }
于 2013-10-30T09:33:18.410 回答
0

根据您的评论,如果您有以下公共财产:

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

在您的 App.xaml.cs 中定义,您可以通过以下方式在每个页面中访问它:

App.MyStaticVariable;

我的观点:如果您谈论的是可以在应用程序启动时定义的 1 个变量,那么独立存储只是矫枉过正。

于 2013-10-31T07:00:31.787 回答