Search code examples
c#windows-phone-8listpicker

lictpicker value is lost in wp8 c#


In my windows phone 8 application I am using listpicker that have two values Dark and Light, when I select Light from listpicker and restart my application, listpicker value i.e Light is lost and it holds default value i.e Dark.

And below is listpicker selectionChanged method:

private void themelistPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string themename = string.Empty;
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            ListPickerItem lpi = (sender as ListPicker).SelectedItem as ListPickerItem;
            themename = lpi.Content.ToString();
            value = lpi.Content.ToString();
            if (themename == "Dark")
            {
                if (!settings.Contains("userData"))
                {
                    settings.Add("userData", themename);
                }
                else
                {
                    settings["userData"] = themename;
                }
                settings.Save(); 
            }
            else
            {

                if (!settings.Contains("userData"))
                {
                    settings.Add("userData", themename);
                }
                else
                {
                    settings["userData"] = themename;
                }
                settings.Save();
            }
        }  

What can I do, Kindly suggest me. Waiting for reply.

Thanks


Solution

  • try this:

    XAML:

            <toolkit:ListPicker x:Name="themelistPicker1" SelectionChanged="themelistPicker1_SelectionChanged">
                <toolkit:ListPickerItem Content="Light"></toolkit:ListPickerItem>
                <toolkit:ListPickerItem Content="Dark"></toolkit:ListPickerItem>
            </toolkit:ListPicker>
    

    CS:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        if (settings.Contains("userData"))
        {
            string str = settings["userData"].ToString();
            if (str == "Dark")
                themelistPicker1.SelectedIndex = 1;
            else
                themelistPicker1.SelectedIndex = 0;
        }
        base.OnNavigatedTo(e);
    }
    private void themelistPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (themelistPicker1 != null && themelistPicker1.SelectedIndex > -1)
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            ListPickerItem lpi = (sender as ListPicker).SelectedItem as ListPickerItem;
            string themename = lpi.Content.ToString();
            if (!settings.Contains("userData"))
                settings.Add("userData", themename);
            else
                settings["userData"] = themename;
            settings.Save();
        }
    }