Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save my app settings?

Tags:

c#

windows

uwp

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...

like image 276
ja13 Avatar asked Feb 09 '23 10:02

ja13


1 Answers

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"];
    }
}
like image 93
user5525674 Avatar answered Feb 10 '23 22:02

user5525674