我已经使用自定义渲染器为Android创建了自定义单选按钮,如下所示 . 以下警告显示在输出窗口中 . 我尝试了很多方法,并提到了许多网站,但我没有得到任何明确的解决方案来修复此警告 . 任何人都可以在不改变nuget版本的情况下给出建议 .
public static BindableProperty ItemsSourceProperty =
BindableProperty.Create<BindableRadioGroup, IEnumerable>
(o => o.ItemsSource, default(IEnumerable), propertyChanged: OnItemsSourceChanged);
public static BindableProperty SelectedIndexProperty =
BindableProperty.Create<BindableRadioGroup,
int>(o => o.SelectedIndex, default(int), BindingMode.TwoWay,
propertyChanged: OnSelectedIndexChanged);
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public int SelectedIndex
{
get { return (int)GetValue(SelectedIndexProperty); }
set { SetValue(SelectedIndexProperty, value); }
}
private static void OnItemsSourceChanged(BindableObject bindable, IEnumerable oldvalue, IEnumerable newvalue)
{
var radButtons = bindable as BindableRadioGroup;
radButtons.rads.Clear();
radButtons.Children.Clear();
if (newvalue != null)
{
int radIndex = 0;
foreach (var item in newvalue)
{
var rad = new CustomRadioButton();
rad.Text = item.ToString();
rad.Id = radIndex;
rad.CheckedChanged += radButtons.OnCheckedChanged;
radButtons.rads.Add(rad);
radButtons.Children.Add(rad);
radIndex++;
}
}
}
private static void OnSelectedIndexChanged(BindableObject bindable, int oldvalue, int newvalue)
{
if (newvalue == -1) return;
var bindableRadioGroup = bindable as BindableRadioGroup;
foreach (var rad in bindableRadioGroup.rads)
{
if (rad.Id == bindableRadioGroup.SelectedIndex)
{
rad.Checked = true;
}
}
}
2 回答
警告消息明确指出不推荐使用BindableProperty.Create的通用版本 .
使用非通用版本,这是一个基于
ItemsSourceProperty
的示例:因此,只需重写BindableProperty声明或禁止警告(不推荐) .
如果您仍不确定如何操作,请参阅official guide .
谢谢你的帮助@EvZ,我已经清除了这样的警告,