自定义控件的数据绑定不起作用



我创建了一个具有自定义属性的自定义组件。问题是,当我试图通过使用它将某个东西绑定到组件时,它将不会编译,并引发错误。

错误:

[XFC0009] No property, BindableProperty, or event found for "Label", or mismatching type between value and property.

Xaml:成分

<?xml version="1.0" encoding="utf-8"?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodel="clr-namespace:TestProject.Components"
x:DataType="viewmodel:Tile"
x:Class="TestProject.Components.Tile">
<Frame CornerRadius="10"
WidthRequest="300"
HeightRequest="150">
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="TileClicked"/>
</Frame.GestureRecognizers>
<StackLayout x:Name="SL_Content"
HorizontalOptions="Start"
VerticalOptions="Start">
<Label Text="{Binding Label}" 
FontSize="{Binding Size}" 
FontAttributes="Bold"/>
<Image Source="{Binding Icon}"
HorizontalOptions="Start"
WidthRequest="50"
HeightRequest="0"
Margin="0,10,0,0"
x:Name="I_Img"/>
</StackLayout>
</Frame>
</ContentView>

不可绑定的实验室属性代码:

private static readonly BindableProperty LabelProperty = BindableProperty.Create(
propertyName: nameof(Label),
returnType: typeof(string),
defaultValue: string.Empty,
declaringType: typeof(Tile),
defaultBindingMode: BindingMode.OneWay);

public string Label
{
get => (string)GetValue(LabelProperty);
set => SetValue(LabelProperty, value);
}

施工单位:

public Tile()
{
BindingContext = this;
InitializeComponent();
}

控件的用途:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:components="clr-namespace:TestProject.Components"
xmlns:model="clr-namespace:TestProject.Model"
xmlns:viewmodel="clr-namespace:TestProject.ViewModel"
x:DataType="viewmodel:ScannedItemViewModel"
x:Class="TestProject.Pages.Popups.CodeDetected">

<CollectionView ItemsSource="{Binding _scannedItems}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:ScannedItem">
<SwipeView>
<SwipeView.RightItems>
<SwipeItem Text="Delete"
Invoked="SwipeItem_OnDelete"/>
</SwipeView.RightItems>
<HorizontalStackLayout HeightRequest="40">
<Label VerticalOptions="Center" HorizontalOptions="Start" WidthRequest="120"
Padding="15,0,0,0" FontSize="16" Text="{Binding _num}" />
<Label VerticalOptions="Center" HorizontalOptions="Start" FontSize="16"
Text="{Binding _serial}" />

无法像这样将某些东西绑定到Lable,则会抛出错误

<components:Tile Label="{Binding _serial}"/>
</HorizontalStackLayout>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

通常,只告诉您的视图它需要自己查找绑定是很有意义的:

<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodel="clr-namespace:TestProject.Components"
x:DataType="viewmodel:Tile"
x:Name"this"
x:Class="TestProject.Components.Tile">

然后像这样更改绑定:

<Label Text="{Binding Label}" 
FontSize="{Binding Size, Source={x:Reference this}}" 
FontAttributes="Bold"/>
<Image Source="{Binding Icon, Source={x:Reference this}}"
HorizontalOptions="Start"
WidthRequest="50"
HeightRequest="0"
Margin="0,10,0,0"
x:Name="I_Img"/>

此外,BindingContext在自定义控件中这样做总是一个错误,当您在视图中使用它时,它会干扰控件的VM分配,所以请将构造函数更改为

public Tile()
{
InitializeComponent();
}

祝你好运!

您只需要将private更改为public,例如:

public static readonly BindableProperty LabelProperty = BindableProperty.Create(
propertyName: nameof(Label),
returnType: typeof(string),
defaultValue: string.Empty,
declaringType: typeof(Tile),
defaultBindingMode: BindingMode.OneWay);

我遇到过这个问题,在官方文件中,它也是公开的。

最新更新