Xamarin表单中的可重用ContentView

时间:2018-10-29 22:09:39

标签: 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"
          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,我打算在其中重用上面创建的内容视图。

<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>

如果我使用静态值,则效果很好。但是,如果我绑定值

<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

如何在此处进行绑定。由于我的值未知,因此无法在XAML中分配策略值,因此只能使用Binding。 我在这里应该怎么做才能约束价值,请帮助我。

1 个答案:

答案 0 :(得分:2)

我认为您的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”。因此,尝试:

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方法的第3个参数是declaringType,它应该是定义可绑定属性的类型。

示例项目: https://www.dropbox.com/s/j7kpehpt8htrf8k/TestAlertView.zip?dl=0