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

时间:2017-12-22 10:41:35

标签: xamarin xamarin.forms xamarin.ios xamarin.android

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="select1">
  <option value='0'>select ....</option>
  <option value='1'>1</option>
  <option value='2'>2</option>
  <option value='3'>3</option>
</select>