如何从我的选择器中获取价值?Xamarin形成

Did*_*nDo 4 c# xamarin xamarin.forms

我很难string从我的选择器中取出所选的.这是我的代码:

XAML:

<Picker x:Name="thePicker" >
    <Picker.Items>
        <x:String>info1</x:String>
        <x:String>info2 </x:String>
    </Picker.Items>
</Picker>
Run Code Online (Sandbox Code Playgroud)

码:

thePicker.SelectedIndex = 1; //here is the problem i suppose, any idea what i should type?
var ourPickedItem = thePicker.Items[thePicker.SelectedIndex];
Run Code Online (Sandbox Code Playgroud)

现在我只得到值"info1",即使我选择了数字2.这与SelectedIndex它有关,所以它只选择"1",但我不知道如何编写代码使其适用于所选项目.

Ale*_*aro 12

你应该看看这个:

picker.SelectedIndexChanged += (sender, args) =>
{
    if (picker.SelectedIndex == -1)
    {
        boxView.Color = Color.Default;
    }
    else
    {
        string colorName = picker.Items[picker.SelectedIndex];
        boxView.Color = nameToColor[colorName];
    }
};
Run Code Online (Sandbox Code Playgroud)

否则在新的Xamarin Forms Release 2.3.4中存在

可绑定的选择器

你可以设置一个ItemSource

<Picker ItemsSource="{Binding Countries}" />
Run Code Online (Sandbox Code Playgroud)

并绑定SelectedIndex属性

SelectedIndex="{Binding CountriesSelectedIndex}"
Run Code Online (Sandbox Code Playgroud)