私は設定クラスを持っています。このクラスは、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;
}
私の問題は、アプリを閉じて再度起動すると、値がデフォルト値に戻ることです。
これが事実である理由がわかりません。