C# Xamarin表单中的可重用ContentView

C# Xamarin表单中的可重用ContentView,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我已经分别创建了一个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"

我已经分别创建了一个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>
这是我的ContentPage.Xaml示例,我计划在其中重用上面创建的内容视图

<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>
我怎样才能在这里装订呢。因为我的值是未知的,所以我不能在XAML中分配stactic值,我只能使用绑定。
我应该在这里做什么来绑定值,请帮助我

我认为您的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
。根据惯例(不确定是否需要),可绑定属性名称将是属性名称+属性。您的属性名为+“TextProperty”。因此,请尝试:

有关更多信息,请参阅。您将看到
BindableProperty.Create
方法的第三个参数是
declaringType
,它应该是定义BindableProperty的类型


示例项目:

您绑定到的“sample”属性的类型是什么?sample只是一个字符串值我尝试了您提到的方法。但还是一样的问题。我看到了很多可重用的内容视图示例,它们都在XAML中指定了静态值。不绑定任何值。我只是确认这在我这边有效。我添加了一个链接到一个示例项目,该项目演示了在做出我所注意到的更改后绑定的工作情况。OMG!!!我不知道错在哪里。但是从你的链接复制代码,效果很好。我浪费了一天时间来解决这个问题:-(
<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
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 static readonly BindableProperty HeadingProperty = 
    BindableProperty.Create("Heading", typeof(string), typeof(AlertView));
public static readonly BindableProperty MessageProperty = 
    BindableProperty.Create("Message", typeof(string), typeof(AlertView));