C# Xamarin iOS项目中的BindableRadioGroup
我在Xamarin的一个项目中工作。 以前该项目只是Android,现在我需要包括同一应用程序的iOS版本。 在我的应用程序页面中有一个单选按钮的动态列表。我使用的组件是XLabs.Forms包的BindableRadioGroup 这是xaml页面中的标记:C# Xamarin iOS项目中的BindableRadioGroup,c#,ios,xamarin,xamarin.forms,C#,Ios,Xamarin,Xamarin.forms,我在Xamarin的一个项目中工作。 以前该项目只是Android,现在我需要包括同一应用程序的iOS版本。 在我的应用程序页面中有一个单选按钮的动态列表。我使用的组件是XLabs.Forms包的BindableRadioGroup 这是xaml页面中的标记: <ScrollView Orientation = "Vertical" VerticalOptions="Start" HeightRequest="250">
<ScrollView Orientation = "Vertical" VerticalOptions="Start" HeightRequest="250">
<controls:BindableRadioGroup SelectedIndex="{Binding SelectedAnomaly, Mode=TwoWay}" ItemsSource="{Binding AnomalyList, Mode=TwoWay}" />
</ScrollView>
该列表是从数据库动态填充的。
所有操作在Android版本中都能正常工作:
但它在iOS版本中不起作用:
我无法选择列表中的元素。
如何在iOS中解决此问题?还有另一种方法可以创建这个单选按钮动态列表,它可以在Android和iOS版本中工作?现在,Xamarin发布了新的
单选按钮控件。尝试将Xamarin.Forms更新到最新版本,您可以查看文档和类文档
要动态添加单选按钮,可以按如下方式创建它们:
// 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);
}
//创建5个单选按钮
对于(int i=0;i<5;i++)
{
RadioButton RadioButton=新RadioButton();
radioButton.Content=$“Content{i}”;
如果(i==1)//检查第二个
radioButton.IsChecked=true;
MyLayout.Children.Add(单选按钮);
}
我通过将Xamarin.Forms更新到5.0.0.2012版解决了这个问题,并通过在DomainFEModel中引入“selected”属性更改了代码。
这是我的新代码:
XAML:
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;
}
}
private DomainFEModel getselectedanomly()
{
尝试
{
for(int i=0;i<0.Length;i++)
{
如果(异常[i]。选中)
{
返回异常[i];
}
}
返回null;
}
捕获(例外e)
{
返回null;
}
}
<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>
private DomainFEModel[] _anomalies;
public DomainFEModel[] Anomalies
{
get { return _anomalies; }
set { SetProperty(ref _anomalies, value); }
}
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;
}
}