如何在Xamarin中绑定DatePicker和Picker [英] How to bind DatePicker and Picker in Xamarin

查看:98
本文介绍了如何在Xamarin中绑定DatePicker和Picker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Xamarin上的Web API在我的应用程序上注册用户. 这是 RegisterPage.xaml

I am trying to register a user on my application using web API on Xamarin. Here is registration form in RegisterPage.xaml

<Entry Text="{Binding FirstName}" Placeholder="First Name"/>
<Entry Text="{Binding LastName}" Placeholder="Last Name"/>
<Entry Text="{Binding UserName}" Placeholder="Username"/>
<Entry Text="{Binding Email}" Placeholder="Email" />
<Entry Text="{Binding Password}" Placeholder="Password" IsPassword="True"/>
<Entry Text="{Binding ConfirmPassword}" Placeholder="Confirm Password" IsPassword="True"/>

<DatePicker x:Name="BirthDay" MinimumDate="1/1/1948" MaximumDate="12/31/2007"/>

<Label Text="Gender"/> 
<Picker x:Name="GenderPicker" SelectedIndexChanged="GenderPicker_OnSelectedIndexChanged"/>

<Label Text="User Role"/>
<Picker x:Name="RolePicker" SelectedIndexChanged="RolePicker_OnSelectedIndexChanged"/>


<Button Command="{Binding RegisterCommand}" Text="Register"/>
<Label Text="{Binding Message}" />

这是我的 RegisterPage.xaml.cs 文件

public partial class RegisterPage : ContentPage
{
    public RegisterPage()
    {
        InitializeComponent();

        GenderPicker.Items.Add("Male");
        GenderPicker.Items.Add("Female");

        RolePicker.Items.Add("Admin");
        RolePicker.Items.Add("Participant");

    }

    private void GenderPicker_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        var gender = GenderPicker.Items[GenderPicker.SelectedIndex];
    }

    private void RolePicker_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        var role = RolePicker.Items[RolePicker.SelectedIndex];
    }

}

提交注册表时,我没有收到针对 DatePiker Picker 的值到RegisterCommand

On submitting the registration form I am not receiving my values against DatePiker and Picker into the RegisterCommand

这是我的 RegisterCommand ,我正在接收其他属性,但不是从 DatePiker Picker

Here is my RegisterCommand , I am receiving other attributes but not from DatePiker and Picker

public ICommand RegisterCommand
    {
        get
        {
            return new Command(async () =>
            {
                var isSuccess = await _apiServices.RegisterAsync(FirstName,LastName,UserName,Email,Password,ConfirmPassword,BirthDate,Gender,UserRole);
                Message = isSuccess ? "Registered Successfully" : "Retry Later";
            });
        }
    }

推荐答案

从Xamarin Forms 2.3.4开始,您终于将Binding绑定到Picker.您只需要绑定到SelectedItem,如下所示.

Starting in Xamarin Forms 2.3.4 thy finally introduced Binding to the Picker. You only need to bind to the SelectedItem as shown below.

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:local="clr-namespace:PickerDemo" 
             x:Class="PickerDemo.PickerDemoPage">
    <ContentPage.BindingContext>
        <local:PickerDemoPageViewModel />
    </ContentPage.BindingContext>
    <StackLayout Padding="10,20">
        <!-- Picker with explicitly set values in XAML -->
        <Picker SelectedItem="{Binding SelectedItem}">
            <Picker.Items>
                <x:String>Item 1</x:String>
                <x:String>Item 2</x:String>
            </Picker.Items>
        </Picker>
        <Label Text="{Binding SelectedItem,StringFormat='You Selected: {0}'}" />

        <!-- Picker with Dynamically set values -->
        <Picker ItemsSource="{Binding SomeCollection}" SelectedItem="{Binding AnotherItem}" />
        <Label Text="{Binding AnotherItem,StringFormat='You picked: {0}'}" />

        <!-- Date Picker using a custom display format -->
        <DatePicker Date="{Binding SomeDate}" Format="MMMM dd, yyyy" />
        <Label Text="{Binding SomeDate,StringFormat='{0:dd MMMM, yyyy}'}" />
    </StackLayout>

</ContentPage>

这篇关于如何在Xamarin中绑定DatePicker和Picker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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