0

我问自己是否有更好的方法来绑定嵌套属性。

设置是:自定义页面内的自定义视图内的自定义控件。

所以:CustomControl -> CustomView -> CustomPage

CustomControl 有一个条目,我最终希望该条目的 TextProperty 可用于从 CustomPage 绑定。

我现在拥有的是 CustomControl 内的 BindableProperty,它绑定到 CustomView 内的属性,该属性绑定到 CustomView 内的 BindableProperty,该属性绑定到 CustomPage 内的属性,该属性绑定到 CustomPage 内的 BindableProperty。

所以流程是:BindableProperty(CustomControl) -> Property(CustomView) -> BindableProperty(CustomView) -> Property(CustomPage) -> BindableProperty(CustomPage)

这是正确的方法吗?对不起,令人困惑的文字。

一些可视化: 在此处输入图像描述

更新 很多代码公司。

控制,xaml:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:controls="removed"
         x:Class="removed"
         x:Name="this">
<ContentView.Content>
        <controls:Entry x:Name="entry" Text="{Binding EntryText, Source={x:Reference this}}"/>
</ContentView.Content>

控制,后面的代码:

public partial class HeaderSearchBarView : ContentView
{
    public event EventHandler CancelButtonClicked;

    public static readonly BindableProperty EntryTextProperty = BindableProperty.Create(
                                                     propertyName: nameof(EntryText),
                                                     returnType: typeof(string),
                                                     declaringType: typeof(HeaderSearchBarView),
                                                     defaultValue: null,
                                                     defaultBindingMode: BindingMode.TwoWay);

    public HeaderSearchBarView()
    {
        InitializeComponent();
    }

    public string EntryText
    {
        get => (string)GetValue(EntryTextProperty);
        set => SetValue(EntryTextProperty, value);
    }
}

内部带有控件的视图:

public partial class HeaderView : ContentView
{
    public static readonly BindableProperty SearchBarTextProperty = BindableProperty.Create(
                                                     propertyName: nameof(SearchBarText),
                                                     returnType: typeof(string),
                                                     declaringType: typeof(HeaderView),
                                                     defaultValue: null,
                                                     defaultBindingMode: BindingMode.TwoWay);

    private HeaderSearchBarView headerSearchBarView = new HeaderSearchBarView();

    public HeaderView()
    {
        InitializeComponent();
        Content = navigationDrawer;

        headerSearchBarView.SetBinding(HeaderSearchBarView.EntryTextProperty, new Binding(path: nameof(SearchBarText), source: this));
    }

    public string SearchBarText
    {
        get => (string)GetValue(SearchBarTextProperty);
        set => SetValue(SearchBarTextProperty, value);
    }
}

带有内部视图的页面,xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:headerView="removed"
         x:Class="removed"
         x:Name="this">
<ContentPage.Content>
    <StackLayout>
        <headerView:HeaderView x:Name="headerView"
                               IsSearchBarButtonVisible="{Binding IsSearchBarButtonVisible, Source={x:Reference this}}"/>
    </StackLayout>
</ContentPage.Content>

带有视图的页面,后面的代码:

public partial class BaseContentPage : ContentPage
{
    public static readonly BindableProperty SearchBarTextProperty = BindableProperty.Create(
                                                     propertyName: nameof(SearchBarText),
                                                     returnType: typeof(string),
                                                     declaringType: typeof(BaseContentPage),
                                                     defaultValue: null,
                                                     defaultBindingMode: BindingMode.TwoWay);

    public BaseContentPage() : base()
    {
        InitializeComponent();
    }

    public string SearchBarText
    {
        get => (string)GetValue(SearchBarTextProperty);
        set => SetValue(SearchBarTextProperty, value);
    }
}

有了这个,我可以绑定到 BaseContentPage 的 SearchBarText,它最终绑定到我的控件中条目的 TextProperty:

<local:BaseContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                   xmlns:controls="removed"
                   x:Class="removed"
                   SearchBarText="{Binding SearchBarText}">

</local:BaseContentPage>

它有效,但似乎过于复杂。也许有更好的方法?

4

0 回答 0