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?