how can i set the selectedIndex in a listPicker which i retrieve for isolatedStorageSettings in windows phone 8

StackOverflow https://stackoverflow.com/questions/21492709

質問

I am developing a windows phone 8 app in which i have to use listPicker control. I need to save the selectedIndex from selected item in listPicker, in isolatedStorageSettings to be able to use it when the app opens. I want the saved index to be the selected index in my listPicker when the apps runs again. I have tried to do this with the onnavigatedto and onnavigatedfrom methods in the page in which i have the control. The problem is when i change se selected item and return back from full mode, the selected item does not change. I had searched this problem heare again and i didn't found the solution yet. How can i solve it?

Sorry for my English

役に立ちましたか?

解決

I followed this settings_sample for the general setup by modifying the ListBox example. I ran into several problems trying to use a ListPicker with isolated storage like this. http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff769510(v=vs.105).aspx

I removed the databindings for the ListPicker, set the SelectedIndex after initializing, and stored the SelectedIndex in isolated storage on SelectionChanged after the first occurrence of loading the page. It's a roundabout solution, but my searches came up empty.

public List<string> daysOfWeek = new List<string>() { "Sunday", "Monday", "etc" };
public int listPickerCounter = 0;
public Settings()
{
    InitializeComponent();
    BuildLocalizedApplicationBar();
    // Fill listPicker with string items
    this.listPicker.ItemsSource = daysOfWeek;
    // Set SelectedIndex = IsolatedStorage Variable
    if (IsolatedStorageSettings.ApplicationSettings.Contains("ListPickerSetting"))
    {
        this.listPicker.SelectedIndex = (int)IsolatedStorageSettings.ApplicationSettings["ListPickerSetting"];
    }           
}

On SelectionChanged update the isolated storage after the first occurrence of loading page.

private void listPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (listPickerCounter > 0 && IsolatedStorageSettings.ApplicationSettings.Contains("ListPickerSetting"))
    {
        IsolatedStorageSettings.ApplicationSettings["ListPickerSetting"] = (int)this.listPicker.SelectedIndex;
    }
    listPickerCounter++;
}

Edit: forgot to add another reference that really helped understand isolated storage. http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj714090(v=vs.105).aspx

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top