簡體   English   中英

在父級中設置綁定時,Xamarin.Forms 綁定到自定義控件不起作用

[英]Xamarin.Forms Binding to Custom Control Not Working When Binding Is Set In Parent

我正在嘗試創建一個簡單的 Xamarin.Forms 自定義控件,但遇到了綁定問題。

這是我最初的自定義控件:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="CubisMobile.Controls.TestControl"
         x:Name="TestControlView">
<Label Text="{Binding TestText}" />

public partial class TestControl : ContentView
{
    public static readonly BindableProperty TestTextProperty = BindableProperty.Create(nameof(TestText), typeof(string), typeof(TestControl));
    public string TestText
    {
        get { return (string)GetValue(TestTextProperty); }
        set { SetValue(TestTextProperty, value); }
    }

    public TestControl()
    {
        InitializeComponent();

        BindingContext = this;
    }
}

我試圖以這種方式使用它:

...
<StackLayout>
    <controls:TestControl TestText="{Binding Title}" />
    <Label Text="{Binding Title}" />
</StackLayout>
...

我添加了第二個標簽來測試 Title 屬性是否工作正常,並且確實如此。 但是文本不會顯示在自定義控件上。 當我設置一個像TestText="Testing"這樣的常量值時,它會正常工作。 我在 StackOverflow 上找到了這個答案,嘗試了以下方法,但它也不起作用(自定義控件 XAML):

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="CubisMobile.Controls.TestControl"
         x:Name="TestControlView">
<Label Text="{Binding Source={x:Reference TestControlView}, Path=TestText}" />

我真的不明白為什么這種綁定不起作用。

你找到的答案很好,我在我的圖書館里做了同樣的事情:

<tabs:TabItem x:Class="Sharpnado.Presentation.Forms.CustomViews.Tabs.UnderlinedTabItem"
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:tabs="clr-namespace:Sharpnado.Presentation.Forms.CustomViews.Tabs;assembly=Sharpnado.Presentation.Forms"
          x:Name="RootLayout">

<ContentView.Content>
    <Grid BackgroundColor="Transparent">

        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Label Style="{StaticResource TabTextHeader}"
               FontFamily="{Binding Source={x:Reference RootLayout}, Path=FontFamily}"
               FontSize="{Binding Source={x:Reference RootLayout}, Path=LabelSize}"
               Text="{Binding Source={x:Reference RootLayout}, Path=Label}"
               TextColor="{Binding Source={x:Reference RootLayout}, Path=UnselectedLabelColor}">

以及背后的代碼:

    public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create(
        nameof(FontFamily),
        typeof(string),
        typeof(TabItem),
        null,
        BindingMode.OneWay);

    public string FontFamily
    {
        get => (string)GetValue(FontFamilyProperty);
        set => SetValue(FontFamilyProperty, value);
    }

我在您顯示的代碼中看到的唯一問題是BindingContext的設置:

public TestControl()
{
    InitializeComponent();

    BindingContext = this; // Remove this line
}

你的代碼我已經測試過了,需要注意幾個地方:

1.假設 ContentView 的類名是TestControl ,您可以嘗試按照您提到的以下代碼:

 <?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:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="CustomeViewApp1.controls.TestControl"
         x:Name="TestControlView"
         >
<ContentView.Content>
    <Label Text="{Binding Source={x:Reference TestControlView}, Path=TestText}" />
</ContentView.Content>

2.去掉代碼BindingContext = this; TestControl.xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TestControl : ContentView
{

    public static readonly BindableProperty TestTextProperty = BindableProperty.Create(nameof(TestText), typeof(string), typeof(TestControl));
    public string TestText
    {
        get { return (string)GetValue(TestTextProperty); }
        set { SetValue(TestTextProperty, value); }
    }

    public TestControl()
    {
        InitializeComponent();

        //BindingContext = this;
    }

}

我使用的測試xaml如下:

  <StackLayout Orientation="Horizontal" HorizontalOptions="Center">
     <controls:TestControl TestText="{Binding Title}"  VerticalOptions="Center"/>
     <Label Text="{Binding Type}" FontSize="Medium" TextColor="#F0BB7F" 
     FontAttributes="Bold" VerticalOptions="Center"/>
  </StackLayout>

你可以在這里查看我測試的完整演示。

提供的答案工作正常。 但是,這些需要您手動設置每個屬性的綁定源。 如果很多屬性需要綁定,這會變得很乏味。

一種更簡單的方法是覆蓋框架公開的 OnChildAdded 事件並在那里設置綁定上下文。 這將自動為添加的任何子項設置綁定上下文。

為此,請執行以下步驟:

  1. 在代碼隱藏文件中添加以下方法:

    protected override void OnChildAdded(Xamarin.Forms.Element child) { base.OnChildAdded(child); //必須為要應用的基本實現調用 child.BindingContext = this; //這為添加的孩子設置綁定上下文}

  2. 在您的 xaml 中,將您的控件綁定到公共可綁定屬性。 例如:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM