Windows 8 SDK(C#)应用程序_已停用并关闭
我有一节设置课。此类从IsolatedStorage获取和设置值Windows 8 SDK(C#)应用程序_已停用并关闭,c#,windows-8,windows-phone-8,C#,Windows 8,Windows Phone 8,我有一节设置课。此类从IsolatedStorage获取和设置值 public Settings() { try { // Get the settings for this application. settings = IsolatedStorageSettings.ApplicationSettings; } catch (Exception e)
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;
}
最后,我希望在应用程序关闭/退出时存储此值。我用这个代码
// 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;
}
我的问题是,当我关闭应用程序并再次启动它时,该值会回到默认值
我看不出这是为什么。这是在模拟器上实现的。唯一的问题是应用程序被停用并关闭。那些人根本不会被解雇。这不是存储问题。您是否仍将事件连接到xaml()或代码中?在更改值后直接存储这些值。这避免了新应用程序生命周期带来的麻烦。
// 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;
}