我正在尝试使用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];
}
}
在提交注册表单时,我没有收到针对 DatePiker 和 Picker 的值到RegisterCommand
这是我的 RegisterCommand ,我正在接收其他属性,但不是来自 DatePiker 和 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";
});
}
}
答案 0 :(得分: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>