4

I have a problem with saving my app settings? I creating windows 10 universal app and I have Slider which value I want to save.

i use this code to save it:

private void musicVolume_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
    {
        ApplicationDataContainer AppSettings = ApplicationData.Current.LocalSettings;
        AppSettings.Values["musicV"] = musicVolume.Value;
    }

And in constuctor of page I have this lines of code:

ApplicationDataContainer AppSettings = ApplicationData.Current.LocalSettings;

        if (AppSettings.Values.ContainsKey("musicV"))
        {
            musicVolume.Value = Convert.ToDouble(AppSettings.Values["musicV"]);
        }

It supposed to show new value when I go to that page, but it doesn't, it alway show last default one. Why it not working and how to make it work?

PS: sorry for my bad english...

ja13
  • 89
  • 1
  • 8
  • 1
    Try writing those values to `Settings.setting` because app.config resets to default set wherever you build the code. – Irshad Dec 02 '15 at 12:33
  • 1
    http://stackoverflow.com/questions/3032492/save-settings-in-a-net-winforms-application – Irshad Dec 02 '15 at 12:34
  • See [this page](https://msdn.microsoft.com/en-us/library/windows/apps/mt299098.aspx) for how to manage app settings in a Windows 10 Universal app – stuartd Dec 02 '15 at 12:49

3 Answers3

6

Rather than in the constructor, do the musicVolume.Value initialization after the Page is loaded by subscribing to the Page Loaded event in the constructor. The Loaded event is the appropriate place to do such initialization.

For example, in the constructor add:

Loaded += Page_Loaded;

And your Loaded event handler as:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    ApplicationDataContainer AppSettings = ApplicationData.Current.LocalSettings;

    if (AppSettings.Values.ContainsKey("musicV"))
    {
        musicVolume.Value = (double)AppSettings.Values["musicV"];
    }
}
user5525674
  • 921
  • 1
  • 7
  • 19
  • Thaks for advise, but what are actually benefits of this way? – ja13 Dec 03 '15 at 11:05
  • 2
    In Loaded event handler you know that the control element (in your case with the name musicVolume) is properly initialized. More info [here](https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.frameworkelement.loaded). – user5525674 Dec 03 '15 at 13:20
1

You could try to encapsulate your settings into a "SettingsManager" class like described in this post: https://stackoverflow.com/a/33754414/1694281

Like that you would be sure to immediately have access to the last assignated value without any synchronisation problem. (And you could also do binding with it.)

Community
  • 1
  • 1
nhuau
  • 11
  • 8
1

Eventually I override OnNavigatedFrom method:

 protected override void OnNavigatedFrom(NavigationEventArgs e)
 {
    ApplicationDataContainer AppSettings = ApplicationData.Current.LocalSettings;
    AppSettings.Values["musicV"] = musicVolume.Value;
 }

Now it save settings when you leave page and it's work. I still don't now why the method from my answer didn't work. Obviously there is some problem with ValueChanged event.

@nhuau: your solution looks good, but I'm beginner and don't figured it out how to apply it on my problem. But I will keep it in mind and sometime in future come back to it.

ja13
  • 89
  • 1
  • 8