首页 > 解决方案 > 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 版本中一切正常:

安卓版

但在 iOS 版本中不起作用:

iOS版

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

标签: c#iosxamarinxamarin.forms

解决方案


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

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

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

推荐阅读