我已经搜索了两天,但无法找到这个问题的答案。我有一个简单的内容页面,其中包含一个可绑定属性:
在XAML中,我有以下代码:
在自定义控件中,类似这样的东西:
在代码后台中,我使用OnPropertyChanged将接收到的值设置为CustomProperty和lbl1之间的值。 CustomProperty的定义方式与我定义MainTextProperty的方式相同。
然而,这并不起作用。我寻找任何可以让我做类似事情的示例,但我没有找到。你们中的任何人知道我如何完成这样的事情吗?我正在接收类型不匹配的问题,即我传递的值与Xamarin.Forms.Binding之间的值不匹配。
public partial class Page1 : ContentPage
{
public static readonly BindableProperty MainTextProperty = BindableProperty.Create<Page1 , string>(p => p.MainText, string.Empty);
public string MainText
{
get
{
return (string)GetValue(MainTextProperty);
}
set
{
SetValue(MainTextProperty, value);
}
}
}
在XAML中,我有以下代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Test.Pages.Page1">
<Grid HorizontalOptions="Fill" VerticalOptions="Fill">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Text="{Binding MainText}" Grid.Row="0"/>
<Label Text="{Binding MainText}" Grid.Row="1"/>
<Label Text="{Binding MainText}" Grid.Row="2"/>
<ctrl:CustomControl CustomProperty="{Binding MainText}" />
<Label Text="{Binding MainText}" Grid.Row="3"/>
</Grid>
</ContentPage>
在自定义控件中,类似这样的东西:
<StackLayout>
<Label x:Name="lbl1" />
</StackLayout>
在代码后台中,我使用OnPropertyChanged将接收到的值设置为CustomProperty和lbl1之间的值。 CustomProperty的定义方式与我定义MainTextProperty的方式相同。
然而,这并不起作用。我寻找任何可以让我做类似事情的示例,但我没有找到。你们中的任何人知道我如何完成这样的事情吗?我正在接收类型不匹配的问题,即我传递的值与Xamarin.Forms.Binding之间的值不匹配。