title: Saving user settings in Metro app using C#
body: <p>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. </p>
  
  <p>The code to save settings:</p>
  
  <pre><code>        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></pre>
  
  <p>The code to read settings:</p>
  
  <pre><code>            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"]; ----
  </code></pre>
  
  <p>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?</p>