c# - Xamarin 表单中的可重用 ContentView

标签 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,我计划在其中重用上面创建的内容 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/

相关文章:

c# - 我如何暗示链接方法的代码契约以避免链接时的多余检查?

c# - 如何自定义 415 状态码的错误信息?

c# - 通过 Activator.CreateInstance 创建可为 null 的对象返回 null

wpf - Powershell 无法加载 Windows.Markup.XamlReader

ios - Xamarin Forms : ImageSource. FromUri 不显示

xamarin - DevForce 支持 Xamarin?

c# - 检查是否至少有一个线程已完成

c# - WP7 GestureListener 上图像宽度/高度的限制

c# - 基于 Windows 8 XAML 的游戏中的键盘处理

c# - 从另一个 ViewModel Xamarin.Forms 更新 ViewModel