簡單的問題,之前問過,但沒有人給我正確的答案,所以我要非常具體。從另一個頁面讀取IsolatedStorage中的設置
我需要在Windows Phone中讀取IsolatedStorage
的兩個設置。設置爲ExitAlert
和OrientationLock
。我已經設置了設置,它們保存得很好,我只需要知道如何從另一頁面讀取它們。變量設置的頁面是Settings.cs
,我需要從MainPage.xaml.cs
中讀取設置。我也需要知道如何只做一些事情,如果變量是真或假。我想我應該使用if-then-else語句,但我只是想仔細檢查。
我的應用程序是用C#編寫的。這是我使用的存儲設置代碼:
using System;
using System.IO.IsolatedStorage;
using System.Diagnostics;
namespace Google_
{
public class AppSettings
{
IsolatedStorageSettings isolatedStore;
// isolated storage key names
const string ExitWarningKeyName = "ExitWarning";
const string OrientationLockKeyName = "OrientationLock";
// default values
const bool ExitWarningDefault = true;
const bool OrientationLockDefault = false;
public AppSettings()
{
try
{
// Get the settings for this application.
isolatedStore = IsolatedStorageSettings.ApplicationSettings;
}
catch (Exception e)
{
Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
}
}
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;
}
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;
}
public void Save()
{
isolatedStore.Save();
}
public bool ExitWarning
{
get
{
return GetValueOrDefault<bool>(ExitWarningKeyName, ExitWarningDefault);
}
set
{
AddOrUpdateValue(ExitWarningKeyName, value);
Save();
}
}
public bool OrientationLock
{
get
{
return GetValueOrDefault<bool>(ExitWarningKeyName, OrientationLockDefault);
}
set
{
AddOrUpdateValue(OrientationLockKeyName, value);
Save();
}
}
}
}
如果有不清楚的地方,只是讓我知道這樣我就可以進一步解釋。謝謝。
我不太明白這是如何工作。我會只用'if(AppSettings.ExitAlert == true)'來獲得一個特定的布爾值...? – JacobTheDev
我已經用示例更新了我的答案 – calum
因此,應該從任何頁面開始工作?編輯:啊!它完美的工作!非常感謝! – JacobTheDev