如何清除Xamarin.Forms中的可绑定属性警告

问题描述 投票:1回答:2

我已经使用自定义渲染器为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;
                }
            }
        }

Displayed Warning Screenshot

xamarin xamarin.forms xamarin.ios xamarin.android
2个回答
1
投票

警告消息明确指出不推荐使用BindableProperty.Create的通用版本。

使用非通用版本,这是基于您的ItemsSourceProperty的示例:

public static readonly BindableProperty ItemsSourceProperty =
            BindableProperty.Create(
                propertyName: nameof(ItemsSource),
                returnType: typeof(IEnumerable)),
                declaringType: typeof(BindableRadioGroup),
                propertyChanged: OnItemsSourceChanged);

因此,只需重写BindableProperty声明或禁止警告(不推荐)。 如果您仍然不确定如何操作,请参阅official guide


0
投票

谢谢你的帮助@EvZ,我已经清除了这样的警告,

 public static BindableProperty ItemsSourceProperty =
        BindableProperty.Create(propertyName: nameof(ItemsSource), 
          returnType: typeof(IEnumerable), declaringType: typeof(BindableRadioGroup), 
          propertyChanged: OnItemsSourceChanged);


private static void OnItemsSourceChanged(BindableObject bindable, object oldvalue, object newvalue)
{
    var radButtons = bindable as BindableRadioGroup;

    radButtons.rads.Clear();
    radButtons.Children.Clear();
    if (newvalue != null)
    {
        var value = newvalue as IEnumerable;
        int radIndex = 0;
        foreach (var item in value)
        {
            var rad = new CustomRadioButton();
            rad.Text = item.ToString();
            rad.Id = radIndex;

            rad.CheckedChanged += radButtons.OnCheckedChanged;

            radButtons.rads.Add(rad);

            radButtons.Children.Add(rad);
            radIndex++;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.