2

我想在本地文件夹中的 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 事件?请问有什么帮助吗?

4

1 回答 1

0

我自己还没有尝试过,但也许你可以查看这个示例(应用程序设置示例): http ://code.msdn.microsoft.com/windowsapps/App-settings-sample-1f762f49

它使用Windows.UI.ApplicationSettings命名空间。

编辑: 阅读这篇文章和 MSDN 上的快速入门后,在我看来您不需要保存文件,因为 LocalSettings 会为您处理这个?你没有看到这种行为吗?

于 2012-10-19T16:58:30.530 回答