Saving user settings in Metro app using C#

Posted by jitendra garg on Stack Overflow See other posts from Stack Overflow or by jitendra garg
Published on 2012-10-19T16:12:43Z Indexed on 2012/10/19 17:01 UTC
Read the original article Hit count: 163

Filed under:
|
|

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.

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;
    }

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"]; ----

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET