【问题标题】:How to bind DatePicker and Picker in Xamarin如何在 Xamarin 中绑定 DatePicker 和 Picker
【发布时间】:2017-05-29 00:32:55
【问题描述】:

我正在尝试使用 Xamarin 上的 Web API 在我的应用程序上注册用户。 这是 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];
    }

}

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

这是我的 RegisterCommand ,我收到了其他属性,但不是来自 DatePikerPicker

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";
            });
        }
    }

【问题讨论】:

  • 需要将picker 1的selecteditem绑定到gender,将picker2的selecteditem绑定到userrole

标签: xamarin datepicker xamarin.forms


【解决方案1】:

从 Xamarin Forms 2.3.4 开始,您最终将绑定引入到 Picker。只需绑定SelectedItem即可,如下图。

<?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>

【讨论】:

  • 它正在引发异常,即附加信息:位置 29:28。找不到名称 SelectedItem 的属性
  • 您的 Xamarin Forms 版本是什么?
  • 如何查看,如何升级?
  • 查看已安装的 NuGet 包。包管理器会显示您正在使用的版本,并让您在需要时进行更新。
  • 我在GitHub 上发布了一个工作示例供您参考。
猜你喜欢
  • 2019-10-22
  • 2018-12-16
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-08
  • 2022-08-15
  • 1970-01-01
相关资源
最近更新 更多