如何从我的选择器中获取价值? Xamarin形式 [英] How to get the value from my picker? Xamarin forms

查看:88
本文介绍了如何从我的选择器中获取价值? Xamarin形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have trouble to get out the selected string from my picker. This is my code:

XAML:

<Picker x:Name="thePicker" >
    <Picker.Items>
        <x:String>info1</x:String>
        <x:String>info2 </x:String>
    </Picker.Items>
</Picker>

代码:

thePicker.SelectedIndex = 1; //here is the problem i suppose, any idea what i should type?
var ourPickedItem = thePicker.Items[thePicker.SelectedIndex];

现在,即使我选择数字2,我也只获得值"info1".它与SelectedIndex有关,因此它只选择"1",但是我不确定如何编写代码它适用于所选项目.

Now I only get the value "info1" even if i select number 2. It has something to do with the SelectedIndex so it only picks the "1", but I am not sure how to write the code to make it work for the selected item.

推荐答案

您应该查看:

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

否则,在新的Xamarin Forms版本2.3.4中存在

Otherwise in new Xamarin Forms Release 2.3.4 exists the

可绑定的选择器

您可以设置一个ItemSource

you can set an ItemSource

<Picker ItemsSource="{Binding Countries}" />

并绑定SelectedIndex属性

and Bind the SelectedIndex property

SelectedIndex="{Binding CountriesSelectedIndex}"

这篇关于如何从我的选择器中获取价值? Xamarin形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆