使用C#在Metro应用程序保存用户设置 [英] Saving user settings in Metro app using C#
问题描述
我想在Metro应用程序编写用户选择的设置,在本地文件夹。我也做了code如下,但它不工作。
I want to write user selected settings in Metro app, in Local Folder. I have done the code as below, but it is not working.
在code键保存设置:
The code to save settings:
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;
}
在code阅读设置:
The code to read settings:
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变量在一个文件中。但是,我无法找到code做到这一点。此外,为Unload事件的好地方这样做,或者我应该把它移到OnNavigatedFrom事件?任何帮助,请?
Clearly I am supposed to save this localSettings variable in a file. But, I am unable to find the code to do that. Also, is Unload event a good place to do it, or should I move it to OnNavigatedFrom event? Any help, please?
推荐答案
我还没有尝试过自己还,但也许你可以看看这个样本(应用程序设置的样品): HTTP://$c$c.msdn.microsoft.com / windowsapps /应用程序 - 设置 - 采样1f762f49
I haven't tried it myself yet, but maybe you could check out this sample (App settings sample): http://code.msdn.microsoft.com/windowsapps/App-settings-sample-1f762f49
它使用 Windows.UI.ApplicationSettings 的命名空间。
编辑: 看完<一href="http://www.$c$cguru.com/csharp/.net/using-local-storage-with-c-and-xaml-in-windows-8-metro-apps.html"相对=nofollow>这个帖子和快速入门 MSDN上,在我看来,你并不需要保存文件,因为LocalSettings处理这个的吗?你没有看到这种行为?
After reading this post and the quickstart on MSDN, it seems to me you do not need to save a file, because the LocalSettings handle this for you? Are you not seeing this behavior?
这篇关于使用C#在Metro应用程序保存用户设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!