如何在xamarin表单中使用单选按钮

Uza*_*eem 8 xamarin xamarin.forms

创建注册页面我需要从用户那里获取以下数据.-First Name -Last Name -Username -Email -Password-出生日期 - 性别 - 用户角色

对于Last to参数,我无法找到如何在xamarin表单中使用单选按钮.以下是我的注册页面代码.

<StackLayout BackgroundColor="#30af91" Padding="60">


<Entry Text="{Binding FirstName}" Placeholder="First Name"/>
<Entry Text="{Binding LastName}" Placeholder="Last Name"/>
<Entry Text="{Binding UserName}" Placeholder="Last Name"/>
<Entry Text="{Binding Email}" Placeholder="Email" />
<Entry Text="{Binding Password}" Placeholder="Password" IsPassword="True"/>
<Entry Text="{Binding ConfirmPassword}" Placeholder="Confirm Password" IsPassword="True"/>
<DatePicker MinimumDate="1/1/1948" MaximumDate="12/31/2007"/>


<!--Radio buttons for Gender
    1. Male   2.Female-->

<!--Radio Buttons for UserRole
    1. Admin  2.Participant-->

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


</StackLayout>
Run Code Online (Sandbox Code Playgroud)

Din*_*iya 8

Xamarin 表单不提供单选按钮。

您可以使用

1)开关

2)选择器

或任何其他组件来满足您的要求

更新

xamarin forms 4.6版本更新引入了单选按钮控件,这里是官方文档

  • “Xamarin.Forms 4.6 引入了新的 RadioButton 控件。您可以在此处找到文档:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/radiobutton” - 谢谢,@jfmg (2认同)

Mr *_*ood 6

我认为有一个更简单的解决方案,它很容易并且不需要库。确实,一个广播组只是一个精美的ListView。您只需要为每个具有IsSelected标志的单选按钮创建一个viewModel并在2张图像之间切换。我需要允许用户选择令牌保留的时间:

XAML

<ListView
    HasUnevenRows="True"
    HorizontalOptions="FillAndExpand"
    ItemsSource="{Binding Durations}"
    ItemSelected="ListView_ItemSelected"
    SelectedItem="{Binding SelectedDuration}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout
                    Orientation="Horizontal">
                    <Image
                        HeightRequest="18"
                        IsVisible="{Binding IsSelected}"
                        Source="radioButtonChecked.png"
                        WidthRequest="18"/>
                    <Image
                        HeightRequest="18"
                        IsVisible="{Binding IsUnselected}"
                        Source="radioButtonUnchecked.png"
                        WidthRequest="18"/>
                    <Label
                        Margin="8,0,0,0"
                        Text="{Binding Caption}"/>
                </StackLayout>
            </ViewCell>
         </DataTemplate>
      </ListView.ItemTemplate>
   </ListView>
Run Code Online (Sandbox Code Playgroud)

我们在内容页面中创建一个列表视图,并监听ItemSelected事件。每个列表项都是一个水平堆栈面板,我们根据所选状态在两个图像之间进行翻转

背后的代码

public partial class LoginPage : ContentPage
{
    LoginPageViewModel LoginPageViewModel { get; }

    public LoginTwoFactorFrequencyPage ()
    {
        BindingContext = LoginPageViewModel = new LoginPageViewModel();

        InitializeComponent ();
    }

    private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        LoginPageViewModel.UpdateSelected(e.SelectedItem as PersistenceDuration);
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 页面后面的代码实例化视图模型,并使用页面视图模型上新选择的项目调用UpdateSelected方法*

单选按钮视图模型

每个单选按钮的视图模型:

public class PersistenceDuration : ViewModelBase
{
    bool isSelected;

    public string Caption { get; set; }
    public TwoFactorTokenPersistenceDuration Duration { get; set; }
    public bool IsSelected
    {
        get => isSelected;
        set
        {
            isSelected = value;
            OnPropertyChanged();
            OnPropertyChanged("IsUnselected");
        }
    }
    public bool IsUnselected => !IsSelected;

    public PersistenceDuration(string caption, TwoFactorTokenPersistenceDuration duration)
    {
        Caption = caption;
        Duration = duration;
        IsSelected = false;
    }
}
Run Code Online (Sandbox Code Playgroud)

单选按钮视图模型包含选择信息和标题。每当选定状态更改时,我们确保触发OnPropertyChanged

页面视图模型

public class LoginPageViewModel : ViewModelBase
{
    PersistenceDuration duration;
    PersistenceDuration selectedDuration;

    public ObservableCollection<PersistenceDuration> Durations { get; }
    public PersistenceDuration SelectedDuration
    {
        get => selectedDuration;
        set
        {
            if (value != null)
            {
                duration = value;
                UpdateSelected(duration);
            }
            OnPropertyChanged();
        }
    }

    public LoginTwoFactorFrequencyViewModel()
    {
        Durations = new ObservableCollection<PersistenceDuration>(
            new List<PersistenceDuration>()
            {
                new PersistenceDuration(AppResources.Save_code__forever, TwoFactorTokenPersistenceDuration.Forever),
                new PersistenceDuration(AppResources.ChatRequireEvery30Days, TwoFactorTokenPersistenceDuration.ThirtyDays),
                new PersistenceDuration(AppResources.ChatRequireEveryLogin, TwoFactorTokenPersistenceDuration.None),
            });
    }

    public void UpdateSelected(PersistenceDuration persistenceDuration)
    {
        foreach (var item in Durations)
            item.IsSelected = persistenceDuration == item;
    }
}
Run Code Online (Sandbox Code Playgroud)

在页面视图模型中,我们创建XAML绑定到的单选按钮视图模型的列表。当我们UpdateSelected()时,所有IsSelected状态都会更新,这会触发绑定更新,从而翻转图像。

当有人选择一个项目时,您仍然需要对突出显示做一些事情,但这很容易在互联网上找到:)