How can I set List picker in settings page and save and get value from IsolatedStorageSettings? (Descriptions inside)

I don't understand how I was wrong about the list compiler and storing the value in IsolStorageSettings.

I tried,

Settings.cs

    // Our isolated storage settings
    IsolatedStorageSettings isolatedStore;

    // The isolated storage key names of our settings
    const string ListPickerSettingKeyName = "ListPickerSetting";

    // The default value of our settings
    const int ListPickerSettingDefault = 0;

    /// <summary>
    /// Constructor that gets the application settings.
    /// </summary>
    public Settings()
    {
        try
        {
            // Get the settings for this application.
            isolatedStore = IsolatedStorageSettings.ApplicationSettings;

        }
        catch (Exception e)
        {
            Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
        }
    }

    /// <summary>
    /// Property to get and set a ListPicker Setting Key.
    /// </summary>
    public int ListPickerSetting
    {
        get
        {
            return GetValueOrDefault<int>(ListPickerSettingKeyName, ListPickerSettingDefault);
        }
        set
        {
            AddOrUpdateValue(ListPickerSettingKeyName, value);
            Save();
        }
    }

    /// <summary>
    /// Get the current value of the setting, or if it is not found, set the 
    /// setting to the default setting.
    /// </summary>
    /// <typeparam name="valueType"></typeparam>
    /// <param name="Key"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
    {
        valueType value;

        // If the key exists, retrieve the value.
        if (isolatedStore.Contains(Key))
        {
            value = (valueType)isolatedStore[Key];
        }
        // Otherwise, use the default value.
        else
        {
            value = defaultValue;
        }

        return value;
    }


    /// <summary>
    /// Save the settings.
    /// </summary>
    public void Save()
    {
        isolatedStore.Save();
    }

      

in My MainPage.xaml,

    <toolkit:ListPicker Name="lstSetting" SelectionMode="Single" Foreground="DarkBlue" SelectedIndex="{Binding Source={StaticResource Settings}, Path=ListPickerSetting, Mode=TwoWay}" >
                        <toolkit:ListPickerItem Content="item1" />
                        <toolkit:ListPickerItem Content="item2" />
                    </toolkit:ListPicker>

      

The actual problem is that it doesn't display item2 when the selected index is 1,

Please tell me how to display the selected item in the selected index.,.

+3


source to share


1 answer


It looks like the path doesn't match the property name. Shouldn't it Path=Setting

be Path=ListPickerSetting

?



In addition, you must implement INotifyPropertyChanged in the settings so that when a property changes, the control knows that it needs to get a new value.

+1


source







All Articles