繁体   English   中英

有什么方法可以知道何时更改IsolatedStorageSettings.ApplicationSettings中的变量?

[英]Is there any way to know when the variables in IsolatedStorageSettings.ApplicationSettings are changed ?

我正在尝试为Windows Phone 7应用程序创建设置页,并且我将本文作为基础http://msdn.microsoft.com/zh-cn/library/ff769510(v=vs.92).aspx ,我的应用程序类是:

 public class AppSettings
{

    // Our isolated storage settings
    IsolatedStorageSettings isolatedStore;

    // The isolated storage key names of our settings
    const string CheckBoxSettingKeyName = "CheckBoxSetting";
    const string ListBoxSettingKeyName = "ListBoxSetting";
    const string RadioButton1SettingKeyName = "RadioButton1Setting";
    const string RadioButton2SettingKeyName = "RadioButton2Setting";
    const string RadioButton3SettingKeyName = "RadioButton3Setting";
    const string UsernameSettingKeyName = "UsernameSetting";
    const string PasswordSettingKeyName = "PasswordSetting";

    // The default value of our settings
    const bool CheckBoxSettingDefault = true;
    const int ListBoxSettingDefault = 0;
    const bool RadioButton1SettingDefault = true;
    const bool RadioButton2SettingDefault = false;
    const bool RadioButton3SettingDefault = false;
    const string UsernameSettingDefault = "";
    const string PasswordSettingDefault = "";

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

        }
        catch (Exception e)
        {
            Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
        }
    }

    /// <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 (isolatedStore.Contains(Key))
        {
            // If the value has changed
            if (isolatedStore[Key] != value)
            {
                // Store the new value
                isolatedStore[Key] = value;
                valueChanged = true;
            }
        }
        // Otherwise create the key.
        else
        {
            isolatedStore.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="valueType"></typeparam>
    /// <param name="Key"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
    {
        valueType value;

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

        return value;
    }


    /// <summary>
    /// Save the settings.
    /// </summary>
    public void Save()
    {
        isolatedStore.Save();
    }


    /// <summary>
    /// Property to get and set a CheckBox Setting Key.
    /// </summary>
    public bool CheckBoxSetting
    {
        get
        {
            return GetValueOrDefault<bool>(CheckBoxSettingKeyName, CheckBoxSettingDefault);
        }
        set
        {
            AddOrUpdateValue(CheckBoxSettingKeyName, value);
            Save();
        }
    }


    /// <summary>
    /// Property to get and set a ListBox Setting Key.
    /// </summary>
    public int ListBoxSetting
    {
        get
        {
            return GetValueOrDefault<int>(ListBoxSettingKeyName, ListBoxSettingDefault);
        }
        set
        {
            AddOrUpdateValue(ListBoxSettingKeyName, value);
            Save();
        }
    }


    /// <summary>
    /// Property to get and set a RadioButton Setting Key.
    /// </summary>
    public bool RadioButton1Setting
    {
        get
        {
            return GetValueOrDefault<bool>(RadioButton1SettingKeyName, RadioButton1SettingDefault);
        }
        set
        {
            AddOrUpdateValue(RadioButton1SettingKeyName, value);
            Save();
        }
    }


    /// <summary>
    /// Property to get and set a RadioButton Setting Key.
    /// </summary>
    public bool RadioButton2Setting
    {
        get
        {
            return GetValueOrDefault<bool>(RadioButton2SettingKeyName, RadioButton2SettingDefault);
        }
        set
        {
            AddOrUpdateValue(RadioButton2SettingKeyName, value);
            Save();
        }
    }

    /// <summary>
    /// Property to get and set a RadioButton Setting Key.
    /// </summary>
    public bool RadioButton3Setting
    {
        get
        {
            return GetValueOrDefault<bool>(RadioButton3SettingKeyName, RadioButton3SettingDefault);
        }
        set
        {
            AddOrUpdateValue(RadioButton3SettingKeyName, value);
            Save();
        }
    }

    /// <summary>
    /// Property to get and set a Username Setting Key.
    /// </summary>
    public string UsernameSetting
    {
        get
        {
            return GetValueOrDefault<string>(UsernameSettingKeyName, UsernameSettingDefault);
        }
        set
        {
            AddOrUpdateValue(UsernameSettingKeyName, value);
            Save();
        }
    }

    /// <summary>
    /// Property to get and set a Password Setting Key.
    /// </summary>
    public string PasswordSetting
    {
        get
        {
            return GetValueOrDefault<string>(PasswordSettingKeyName, PasswordSettingDefault);
        }
        set
        {
            AddOrUpdateValue(PasswordSettingKeyName, value);
            Save();
        }
    }


}

一切工作正常,我可以从xaml设置页面更新设置值,并且可以使用以下命令在主应用程序中访问设置值

 IsolatedStorageSettings Settings; 
 Settings = IsolatedStorageSettings.ApplicationSettings;
 string checkboxvalue = (string)Settings["CheckBoxSetting"];

现在,我想要的是能够知道何时更改/更新应用程序设置中的值,以便我可以在更新设置时执行操作。

与其他答案相似,但更为简洁。 如果已经使用属性,请实现INotifyPropertyChanged接口,编写自己的RaisePropertyChanged / NotifyPropertyChanged并在目标类中订阅PropertyChanged事件。

IsolatedStorageSettings.ApplicationSettings是一个字典。 默认情况下是不可观察的。

可以通过多种方式执行此操作,但最简单的入门方法是仅在需要了解设置时查询设置。

另一个简单的替代方案是让您的AppSettings对象在更改某些内容时引发事件,并让其他相关类订阅它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM