我正在尝试为我的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"];
现在我想知道的是能够知道应用程序设置中的值何时被更改/更新,以便我可以在更新设置时执行操作。
答案 0 :(得分:1)
与其他答案类似但更简洁。如果您已经使用了属性,请实现 INotifyPropertyChanged 界面,编写您自己的 RaisePropertyChanged / NotifyPropertyChanged ,并在目标类中订阅 PropertyChanged 事件。
IsolatedStorageSettings.ApplicationSettings 是一个词典。默认情况下无法观察到。
答案 1 :(得分:-1)
您可以通过多种方式执行此操作,但最简单的方法是在您需要了解设置时查询设置。
另一个简单的替代方案可以让你的AppSettings
对象在事情发生变化时引发事件并让相关的其他类订阅它们。