简体   繁体   English

如何在设置页面中设置列表选择器并从IsolatedStorageSettings存储和检索值?(内部描述)

[英]How to set List picker in settings page and store and retrieve value from IsolatedStorageSettings?(Descriptions Inside)

I don't get it how did I went wrong in the list picker and saving the value in IsolatedStorageSettings, 我不明白我是如何在列表选择器中出错并将值保存在IsolatedStorageSettings中,

I've tried, 我试过了,

Settings.cs 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, 在我的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 it is not displayed the item2 when the selected index is 1, 实际问题是当所选索引为1时,它不显示item2,

please somebody tell me how to display the selected item in the selected index. 请有人告诉我如何在所选索引中显示所选项目。 . .

It looks like the Path doesn't match the property name. 看起来Path与属性名称不匹配。 Shouldn't Path=Setting be Path=ListPickerSetting instead? 不应该Path=SettingPath=ListPickerSetting吗?

Also, Settings needs to implement INotifyPropertyChanged so that when the property changes, the control will know it needs to get the new value. 此外,Settings需要实现INotifyPropertyChanged,以便在属性更改时,控件将知道它需要获取新值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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