简体   繁体   English

Xamal中的Xamarin Forms Picker项目源绑定

[英]Xamarin Forms Picker Item source binding in Xaml

I need to create states picker in my Xamarin Forms. 我需要在我的Xamarin Forms中创建状态选择器。 I am using Xaml file for creating views. 我正在使用Xaml文件来创建视图。

Can any one help me to bind Picker in Xaml with item source? 任何人都可以帮我绑定Xaml中的Picker和项目源吗?

The XLabs has an excellent example of a bindable picker that I have used in several projects to great affect: XLabs有一个很好的可绑定拾取器示例,我在几个项目中使用了很大的影响:

https://github.com/XLabs/Xamarin-Forms-Labs https://github.com/XLabs/Xamarin-Forms-Labs

This will allow you to replicate the 'ItemsSource' functionality of the Listview. 这将允许您复制Listview的'ItemsSource'功能。

You won't be able to do this in XAML, as you can see here . 您将无法在XAML中执行此操作,如此处所示

You'll have to load the data up in the code behind, using either their regular API, or something like this . 你必须加载在后面的代码中的数据了,即使用他们的常规API,或像这样

You can also serialize your list as a JSON or preferred format, and deserialize that and pass it to the Picker . 您还可以将列表序列化为JSON或首选格式,然后对其进行反序列化并将其传递给Picker

This functionality did not previous exist, but it was recently added to the regular Xamarin.Forms Picker via the new ItemsSource and SelectedItem properties. 此功能以前不存在,但最近通过新的ItemsSourceSelectedItem属性将其添加到常规Xamarin.Forms Picker中。 It is currently in the pre-release NuGet package for version 2.3.4-pre1 , but should be in the stable 2.3.4+ versions once it is released. 它目前在版本2.3.4-pre1的预发布版NuGet包中,但一旦发布,应该是稳定的2.3.4+版本。

As JordanMazurke commented, XLabs has it. 正如JordanMazurke评论的那样,XLabs拥有它。 Here is an example: 这是一个例子:

<ContentPage x:Class="XLabs.Samples.Pages.Controls.ExtendedPickerPage"
         xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:controls="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms"
         Title="Picker">
<ContentPage.Content>
    <StackLayout x:Name="myStackLayout">
        <Label Text="Xaml:" />
        <controls:ExtendedPicker x:Name="myPicker"
                                 DisplayProperty="FirstName"
                                 ItemsSource="{Binding MyDataList}"
                                 SelectedItem="{Binding TheChosenOne}" />
    </StackLayout>
</ContentPage.Content>

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

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