简体   繁体   中英

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 ie Light is lost and it holds default value ie 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

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();
    }
}

You are saving selected value from ListPicker not the state of control. Add code to your Loaded or NavigateTo events of page that contains ListPicker and if Light is present in IsolatedStorageSettings, then programatically change selected index...

If you store this kind of settings in IsolatedStorage, you can just add/remove key without checking value to simplify it. So when the user selects Light, you add some key to storage, when dark you delete it. Key presence then mean that Light is selected :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM