我有一个设置类。此类从 IsolatedStorages 中获取和设置值
public Settings()
{
try
{
// Get the settings for this application.
settings = IsolatedStorageSettings.ApplicationSettings;
}
catch (Exception e)
{
Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
}
}
public int CookieCountSetting
{
get
{
return GetValueOrDefault<int>(CookieCountSettingKeyName, CookieCountSettingDefault);
}
set
{
if (AddOrUpdateValue(CookieCountSettingKeyName, value))
{
Save();
}
}
}
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;
}
public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
{
valueType value;
// If the key exists, retrieve the value.
if (settings.Contains(Key))
{
value = (valueType)settings[Key];
}
// Otherwise, use the default value.
else
{
value = defaultValue;
}
return value;
}
// 我也有保存功能和一些其他不相关的代码
现在在我的代码中我使用这个类,并从存储中设置一个变量
private Settings settings = new Settings();
public int CookieCount;
public Game()
{
InitializeComponent();
CookieCount = settings.CookieCountSetting;
最终我想在应用程序关闭/退出时存储这个值。我用这个代码
// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
settings.CookieCountSetting = CookieCount;
}
// Code to execute when the application is closing (eg, user hit Back)
// This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e)
{
settings.CookieCountSetting = CookieCount;
}
我的问题是,当我关闭应用程序并重新启动它时,该值又回到了默认值。
我不明白为什么这是事实。