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

Xamarin.forms 如何在自定义contentView中创建可绑定属性,xamarin.forms,Xamarin.forms,我创建了一个带有图像和标签的自定义ContentView 我还创建了ImageUrl和LabelText的属性 我希望此ContentView在listview中接收绑定值 <MyCustomContentView ImageUrl="{Binding Image}" LabelText="{Binding Text}" /> 但是它说没有可绑定属性。如何创建它?您可以使用此代码并将其粘贴到代码隐藏文件中MyCustomContentView类的构造函数下面 pub

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

我还创建了ImageUrl和LabelText的属性

我希望此ContentView在listview中接收绑定值

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

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

您可以使用此代码并将其粘贴到代码隐藏文件中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); }

如果您还有其他困难,请告诉我,您可以使用此代码并将其粘贴到代码隐藏文件中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); }
如果在MyCustomContentView中使用时遇到更多困难,请告诉我。在MyCustomContentView中使用。