如何在自定义contentView中创建可绑定属性

时间:2019-11-06 04:41:58

标签: xamarin.forms

我创建了带有图像和标签的自定义ContentView。

我还创建了ImageUrl和LabelText的属性。

我希望此ContentView在列表视图中接收绑定值

<MyCustomContentView ImageUrl="{Binding Image}" LabelText="{Binding Text}" />

但是它说没有可绑定的属性。如何创建它?

1 个答案:

答案 0 :(得分:2)

您可以使用此代码并将其粘贴到文件后面的代码中MyCustomContentView类的构造函数下方。

        public static readonly BindableProperty LabelTextProperty = BindableProperty.Create(nameof(LabelText), typeof(string), typeof(MyCustomContentView), default(string));
        public static readonly BindableProperty ImageUrlProperty = BindableProperty.Create(nameof(ImageUrl), typeof(string), typeof(MyCustomContentView), default(string));

        public string LabelText { get => (string)GetValue(LabelTextProperty); set => SetValue(LabelTextProperty, value); }
        public string ImageUrl { get => (string)GetValue(ImageUrlProperty); set => SetValue(ImageUrlProperty, value); }

让我知道您是否还有其他困难