无法为自定义 ContentView 创建可绑定属性

问题描述

我创建了一个带有单个标签ContentView(我打算稍后添加更多)。

PageheadingView.xaml

  <ContentView.Content>
        <StackLayout Orientation="Vertical" BackgroundColor="Red">
            <Label x:Name="headingLabel"  Text="{Binding headingText}" />
        </StackLayout>
  </ContentView.Content>

我在后面的代码中定义了一个 BindableProperty。我还将视图的 BindingContext 设置为自身。

PageheadingView.xaml.cs

[XamlCompilation(XamlCompilationoptions.Compile)]
public partial class PageheadingView : ContentView
{
    public PageheadingView()
    {
        InitializeComponent();
        this.BindingContext = this;
    }

    public static readonly BindableProperty headingTextProperty = BindableProperty.Create(nameof(headingText),typeof(string),typeof(PageheadingView),default(string));
    public string headingText { get => (string)GetValue( headingTextProperty); set => SetValue(headingTextProperty,value); }
}

然后我将视图添加到我的 ContentPage。我还在 StackLayout 中添加一个测试标签以确保我的绑定正常工作。

HomePage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:views="clr-namespace:MyProject.Views"
             x:Class="MyProject.HomePage">
    <ContentPage.Content>
        <StackLayout Orientation="Vertical">
            <views:PageheadingView headingText="{Binding Name}" />
            <Label Text="{Binding Name}" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

并在代码中设置我的 BindingContext

HomePage.xaml.cs

[XamlCompilation(XamlCompilationoptions.Compile)]
public partial class HomePage : ContentPage
{
    public HomePage()
    {
        InitializeComponent();
        //viewmodel contains a string property named: Name;
        this.BindingContext = new viewmodel();
    }
}

当我运行我的代码时,我的 PageheadingView 不显示任何文本。我可以看到红色背景色,所以我知道控件已正确添加页面中。我在 StackLayout 中放置的测试 Label 也正常工作,并且可以看到绑定值。

我需要做什么才能让我的 CustomView 显示可绑定的内容

解决方法

您在 PageHeadingView 中绑定了错误的元素。

选项 1:
在 PageHeadingView.xaml.cs 中添加“内容”

this.Content.BindingContext = this;

选项 2:
删除 PageHeadingView.xaml 中的“内容”

  <!--<ContentView.Content>-->
        <StackLayout Orientation="Vertical" BackgroundColor="Red">
            <Label x:Name="HeadingLabel"  Text="{Binding HeadingText}" />
        </StackLayout>
  <!--</ContentView.Content>-->

编辑为之前引用了错误的类。

,

从您的代码来看,您在使用 bindableproperty 出价时可能会遇到一些问题,我创建了一个简单的示例,您可以看看:

PageHeadingView.xaml:

 <ContentView.Content>
    <StackLayout BackgroundColor="Red" Orientation="Vertical">
        <Label x:Name="HeadingLabel" />
    </StackLayout>
</ContentView.Content>

PageHeadingView.cs,可以通过 HeadingTextPropertyChanged 更新 HeadingText 值,然后显示 HeadingLabel。

 public partial class PageHeadingView : ContentView
{

    public static BindableProperty HeadingTextProperty= BindableProperty.Create(
                                                     propertyName: "HeadingText",returnType: typeof(string),declaringType: typeof(PageHeadingView),defaultValue: "",defaultBindingMode: BindingMode.OneWay,propertyChanged: HeadingTextPropertyChanged);
    
    public string HeadingText
    {
        get { return base.GetValue(HeadingTextProperty).ToString(); }
        set { base.SetValue(HeadingTextProperty,value); }
    }
    private static void HeadingTextPropertyChanged(BindableObject bindable,object oldValue,object newValue)
    {
        var control = (PageHeadingView)bindable;
        control.HeadingLabel.Text = newValue.ToString();
    }

    public PageHeadingView()
    {
        InitializeComponent();
    }
}

<StackLayout>
        <customcontrol:PageHeadingView HeadingText="{Binding Name}" />
        <Label Text="{Binding Name}" />
    </StackLayout>