Xamarin.forms 如何将字典密钥对以xamarin形式放入选择器中,并从选择器中检索键值?

Xamarin.forms 如何将字典密钥对以xamarin形式放入选择器中,并从选择器中检索键值?,xamarin.forms,dropdown,code-behind,picker,bindable,Xamarin.forms,Dropdown,Code Behind,Picker,Bindable,我有一个xaml文件,我把选择器放在其中,还有一个代码用来填充它。使用的代码如下: Dictionary<string, int> list = new Dictionary<string, int> { { "Aqua", 6}, { "Red", 1 }, { "Silver", 2 }, { "Teal", 3 }

我有一个xaml文件,我把选择器放在其中,还有一个代码用来填充它。使用的代码如下:

 Dictionary<string, int> list = new Dictionary<string, int>
            {
                { "Aqua", 6},
                { "Red", 1 },
                { "Silver", 2 },
                { "Teal", 3 },
                { "White", 4 },
                { "Yellow", 55 }
            };
            foreach (string item in list.Keys)
            {
                ddlPurpose.Items.Add(item);
            }

甚至可以得到键值吗?我们已经调查了BindablePicker,但这似乎根本不起作用。在此方面的任何帮助都将不胜感激

我想你想做的是:

var pSelectedIndex = ddlPurpose.SelectedIndex;
var selectedKey = list.Values.ElementAt(pSelectedIndex);
我建议您熟悉MVVM,在本例中,还建议您熟悉。我编写了一个小示例来演示如何使用MVVM:

public class PickerKeyValueTestViewModel : INotifyPropertyChanged
{
    static Dictionary<string, int> colors { get; } = new Dictionary<string, int>
        {
            { "Aqua", 6 },
            { "Red", 1 },
            { "Silver", 2 },
            { "Teal", 3 },
            { "White", 4 },
            { "Yellow", 55 }
        };

    public List<string> Colors { get; } = colors.Keys.ToList();

    public string SelectedColor { get; set; }

    public void OnSelectedColorChanged()
    {
        if (string.IsNullOrEmpty(SelectedColor)) return;
        var selectedValue = colors[SelectedColor];
    }

    // Using PropertyChanged.Fody
    public event PropertyChangedEventHandler PropertyChanged;
}

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:PickerKeyValueTest"
    x:Class="PickerKeyValueTest.PickerKeyValueTestPage">

    <ContentPage.BindingContext>
        <local:PickerKeyValueTestViewModel />
    </ContentPage.BindingContext>

    <StackLayout
        Margin="25">
        <Picker
            ItemsSource="{Binding Colors}"
            SelectedItem="{Binding SelectedColor}">

        </Picker>
    </StackLayout>
</ContentPage>
公共类PickerKeyValueTestViewModel:INotifyPropertyChanged
{
静态字典颜色{get;}=新字典
{
{“Aqua”,6},
{“红色”,1},
{“银”,2},
{“Teal”,3},
{“白色”,4},
{“黄色”,55}
};
公共列表颜色{get;}=Colors.Keys.ToList();
公共字符串SelectedColor{get;set;}
选定颜色更改()时的公共无效
{
if(string.IsNullOrEmpty(SelectedColor))返回;
var selectedValue=colors[SelectedColor];
}
//使用PropertyChanged.Fody
公共事件属性更改事件处理程序属性更改;
}

这是一个很好的例子。真的让我对我想做什么和怎么做有了基本的了解。最后,另一个答案有一些额外的信息,可能也会有帮助。
public class PickerKeyValueTestViewModel : INotifyPropertyChanged
{
    static Dictionary<string, int> colors { get; } = new Dictionary<string, int>
        {
            { "Aqua", 6 },
            { "Red", 1 },
            { "Silver", 2 },
            { "Teal", 3 },
            { "White", 4 },
            { "Yellow", 55 }
        };

    public List<string> Colors { get; } = colors.Keys.ToList();

    public string SelectedColor { get; set; }

    public void OnSelectedColorChanged()
    {
        if (string.IsNullOrEmpty(SelectedColor)) return;
        var selectedValue = colors[SelectedColor];
    }

    // Using PropertyChanged.Fody
    public event PropertyChangedEventHandler PropertyChanged;
}

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:PickerKeyValueTest"
    x:Class="PickerKeyValueTest.PickerKeyValueTestPage">

    <ContentPage.BindingContext>
        <local:PickerKeyValueTestViewModel />
    </ContentPage.BindingContext>

    <StackLayout
        Margin="25">
        <Picker
            ItemsSource="{Binding Colors}"
            SelectedItem="{Binding SelectedColor}">

        </Picker>
    </StackLayout>
</ContentPage>