我单独创建了一个 contentview,这样我就可以在不同的 ContentPage 中重用它。
这是我的 ContentView.XAML
<?xml version="1.0" encoding="UTF-8"?> <ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:custom="clr-namespace:MyMXLibrary.Helpers"
x:Class="MyMXLibrary.Views.AlertView"
x:Name="this">
<ContentView.Content>
<Frame VerticalOptions="Center" HorizontalOptions="Center">
<StackLayout>
<Label Text="{Binding Source={x:Reference this}, Path=Heading}"/>
<Label Text="{Binding Source={x:Reference this}, Path=Message}"/><Label Text="Cancel">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding CancelCommand}" />
</Label.GestureRecognizers>
</Label>
<Label Text="Ok">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ProceedCommand}" />
</Label.GestureRecognizers>
</Label>
</StackLayout>
</Frame>
</ContentView.Content></ContentView>
这是我的 ContentView.Xaml.cs
public partial class AlertView : ContentView
{
public static readonly BindableProperty HeadingTextProperty = BindableProperty.Create(nameof(Heading), typeof(string), typeof(Label));
public static readonly BindableProperty MessageTextProperty = BindableProperty.Create(nameof(Message), typeof(string), typeof(Label));
public string Heading { get { return (string)GetValue(HeadingTextProperty); } set { SetValue(HeadingTextProperty, value); } }
public string Message { get { return (string)GetValue(MessageTextProperty); } set { SetValue(MessageTextProperty, value); } }
public AlertView()
{
InitializeComponent();
}
}
这是我的示例 ContentPage.Xaml,我计划在其中重用上面创建的内容 View 。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:alert="clr-namespace:MyMXLibrary.Views"
x:Class="MyMXLibrary.Views.MyPage"
Title="MyPage"><ContentPage.Content><ContentView >
<alert:AlertView Heading="Test" Message="Sample" ></alert:AlertView></ContentView></ContentPage.Content></ContentPage>
如果我使用静态值,它工作正常。但是相反,如果我绑定(bind)值
<alert:AlertView Heading="Test" Message="{Binding sample}" ></alert:AlertView>
我遇到了错误
No property, bindable property, or event found for 'Message', or mismatching type between value and property
我如何在这里进行绑定(bind)。因为我的值是未知的,所以我不能在 XAML 中分配静态值,我必须只使用绑定(bind)。 我应该在这里做什么来绑定(bind)值(value),请帮助我。
最佳答案
我认为您的 BindableProperty 创建代码有误。你有:
public static readonly BindableProperty HeadingTextProperty =
BindableProperty.Create(nameof(Heading), typeof(string), typeof(Label));
public static readonly BindableProperty MessageTextProperty =
BindableProperty.Create(nameof(Message), typeof(string), typeof(Label));
所以第三个参数是 typeof(Label)
但它应该是将具有该属性的类的类型,在本例中为 AlertView
。同样按照惯例(不确定是否需要),可绑定(bind)属性名称将是属性名称 +“Property”。您有属性名称 +“TextProperty”。所以尝试:
public static readonly BindableProperty HeadingProperty =
BindableProperty.Create("Heading", typeof(string), typeof(AlertView));
public static readonly BindableProperty MessageProperty =
BindableProperty.Create("Message", typeof(string), typeof(AlertView));
参见 this获取更多信息。您将看到 BindableProperty.Create
方法的第三个参数是 declaringType
,它应该是定义可绑定(bind)属性的类型。
示例项目: https://www.dropbox.com/s/j7kpehpt8htrf8k/TestAlertView.zip?dl=0
关于c# - Xamarin 表单中的可重用 ContentView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53054608/