0

我正在尝试为我的 windows phone 7 应用程序创建一个设置页面,我使用这篇文章作为基础http://msdn.microsoft.com/en-us/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"];

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

4

2 回答 2

1

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

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

于 2013-02-04T15:02:37.840 回答
-1

有很多方法可以做到这一点,但最简单的入门方法是在需要了解设置时查询设置。

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

于 2012-05-04T08:22:43.127 回答