Xamarin iOS 项目中的 BindableRadioGroup

问题描述

我在 Xamarin 项目中工作。 以前该项目只有 Android,现在我还需要包含同一应用程序的 iOS 版本。 在我的应用程序页面中,有一个单选按钮的动态列表。我使用的组件是 XLabs.Forms 包的 BindableRadioGroup。

这是xaml页面中的标签

<ScrollView  Orientation = "Vertical" VerticalOptions="Start" HeightRequest="250">
    <controls:BindableRadioGroup Selectedindex="{Binding SelectedAnomaly,Mode=TwoWay}" ItemsSource="{Binding AnomalyList,Mode=TwoWay}" />
</ScrollView>

我的 viewmodel 中的代码

public DomainFEModel SelectedAnomalyDomain
        {
            get { return _selectedAnomalyDomain; }
            set { SetProperty(ref _selectedAnomalyDomain,value); }
        }

public ObservableCollection<string> AnomalyList
        {
            get { return _anomalyList; }
            set { SetProperty(ref _anomalyList,value); }
        }

这是前端模型:

public class DomainFEModel : FEModel
{
    public string key { get; set; }
    public string description { get; set; }

}

该列表是从数据库动态填充的。 在 Android 版本中一切正常:

Android version

但它在 iOS 版本中不起作用:

iOS version

我无法选择列表中的元素。 我如何在 iOS 中解决这个问题?还有另一种方法可以创建这个单选按钮动态列表,Android 和 iOS 版本都可以使用吗?

解决方法

现在,Xamarin 发布了新的 RadioButton 控件。尝试将 Xamarin.Forms 更新到最新版本,您可以查看文档 here 和类文档 here

要动态添加单选按钮,您可以按如下方式创建它们:

// create 5 radiobuttons
for (int i = 0; i < 5; i++)
{
    RadioButton radioButton = new RadioButton();
    radioButton.Content = $"Content{i}";
    if (i == 1) // check the 2rd one
        radioButton.IsChecked = true;
    MyLayout.Children.Add(radioButton);
}
,

我通过将 Xamarin.Forms 更新到 5.0.0.2012 版解决了该问题,并通过在 DomainFEModel 中引入“selected”属性更改了代码。 这是我的新代码:

XAML:

<ScrollView  Orientation = "Vertical" VerticalOptions="Start" HeightRequest="300">
                <ListView ItemsSource="{Binding Anomalies}" SelectionMode="Single" SeparatorVisibility="None" BackgroundColor="White">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <RadioButton Content="{Binding description}" Value="{Binding key}" IsChecked="{Binding selected}" GroupName="Anomalies" BackgroundColor="White" TextColor="Black" />
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </ScrollView>

ViewModel 中的变量:

private DomainFEModel[] _anomalies;
    
 public DomainFEModel[] Anomalies
     {
         get { return _anomalies; }
         set { SetProperty(ref _anomalies,value); }
     }

DomainFEModel 类:

 public class DomainFEModel : FEModel
     {
         public string key { get; set; }
         public string description { get; set; }    
         public bool selected { get; set; } = false;
    
     }

获取选中元素的方法:

private DomainFEModel getSelectedAnomaly()
         {
             try
             {
                 for (int i = 0; i < Anomalies.Length; i++)
                 {
                     if (Anomalies[i].selected)
                     {
                         return Anomalies[i];
                     }
                 }
                 return null;
             }
             catch (Exception e)
             {                
                 return null;
             }
         }