我想在本地文件夹中的 Metro 应用程序中写入用户选择的设置。我已经完成了如下代码,但它不起作用。
保存设置的代码:
void OnUnloaded(object sender, RoutedEventArgs args)
{
//code to save app settings.
var localSettings = ApplicationData.Current.LocalSettings;
localSettings.Values["playerPosition"] = playerPosition;
localSettings.Values["aiPosition"] = aiPosition;
localSettings.Values["selectedLevel"] = selectedLevel;
}
读取设置的代码:
var localSettings = ApplicationData.Current.LocalSettings;
if ((localSettings.Values["playerPosition"]) == null)
{
localSettings.Values["playerPosition"] = 1;
localSettings.Values["aiPosition"] = 1;
localSettings.Values["selectedLevel"] = "1";
playerPosition = aiPosition = 1;
selectedLevel = "1";
}
else
{
playerPosition = (int)localSettings.Values["playerPosition"];
aiPosition = (int)localSettings.Values["aiPosition"];
selectedLevel = (string)localSettings.Values["selectedLevel"]; ----
显然,我应该将此 localSettings 变量保存在文件中。但是,我找不到执行此操作的代码。另外,Unload 事件是一个好地方,还是应该将它移到 OnNavigatedFrom 事件?请问有什么帮助吗?