簡體   English   中英

如何將 object 作為可綁定屬性傳遞給 Xamarin.Forms 自定義控件

[英]How to pass object as bindable property to Xamarin.Forms custom control

正如標題所示,我試圖將一個 Person object 傳遞給自定義控件,而不是分別傳遞每個屬性。

所以這:

 <controls:PersonControl 
           Person="{Binding Person}"
           ControlTemplate="{StaticResource PersonControlTemplate}">
   </controls:PersonControl>

而不是這個(有效,基於這個實現)

<controls:PersonControl 
       Name="{Binding Person.Name}"
       Age="{Binding Person.Age}" 
       ControlTemplate="{StaticResource PersonControlTemplate}">
</controls:PersonControl>

我已經嘗試更改后面的 PersonControl 代碼上的可綁定屬性簽名,但它不起作用。 我實際上只是得到一個空白屏幕。

所以:1 - 這甚至可能嗎(我知道它被稱為可綁定屬性,但它是否也需要對象?2 - 如果不是,推薦的方法是什么?

我想這樣做的原因是這個人 object 可能會隨着時間的推移而增長,我寧願只更新自定義控件而不是消費頁面及其視圖 model。

更新:這是 PersonControl 代碼:

public partial class PersonControl : ContentView
    {

        public static readonly BindableProperty PersonProperty = BindableProperty.Create(
            nameof(Person),
            typeof(Person),
            typeof(PersonControl),
            string.Empty);

        public string Name
        {
            get { return this.Person.Name; }
        }

        public Person Person
        {
            get { return (Person)GetValue(PersonProperty); }
            set { SetValue(PersonProperty, value); }
        }

        public PersonControl()
        {
            InitializeComponent();
        } 

    }

這是 PersonControl xaml:

<ContentView.Content>
     <StackLayout>
            <Label  Text="{TemplateBinding Person.Name, Mode=OneWay}"/>
      </StackLayout>
    </ContentView.Content>

最后是消費頁面:

 <ContentPage.Resources>
    <ControlTemplate x:Key="PersonControlTemplate">
        <controls:PersonControl></controls:PersonControl>
    </ControlTemplate>        
</ContentPage.Resources>

 <ContentPage.Content>
    <StackLayout Spacing="10" x:Name="layout">
        <controls:PersonControl
            Person="{Binding Person}"
             ControlTemplate="{StaticResource PersonControlTemplate}"></controls:PersonControl>
              </StackLayout>
</ContentPage.Content>

根據 mvvm 模式,人 object 是頁面視圖模型上的一個屬性。 在此先感謝您的幫助。

更新:我按照本教程嘗試用 object 替換可綁定字符串類型,但仍然沒有任何樂趣

是的,你可以,我使用了Entry ,鍵入一些文本,然后將文本傳輸到ContentView中的 Label。 這是運行 GIF。

在此處輸入圖像描述

首先,我添加一個名為PersonName的屬性,如下面的代碼

  public partial class PersonControl : ContentView
    {
        public PersonControl()
        {
            InitializeComponent();
        }

        public static BindableProperty PersonNameProperty = BindableProperty.Create(
                    propertyName: "PersonName",
                    returnType: typeof(string),
                    declaringType: typeof(PersonControl),
                    defaultValue: "",
                    defaultBindingMode: BindingMode.OneWay);

        public string PersonName
        {
            get { return (string)GetValue(PersonNameProperty); }
            set { SetValue(PersonNameProperty, value); } 
        }
    }

然后,這里是關於ContentView布局的代碼。 請不要忘記在ContentView選項卡中添加x:Name="ParentControl"

<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:Name="ParentControl"
             x:Class="CustomDemoControl.PersonControl">
  <ContentView.Content>
        <StackLayout>
            <Label  Text="{Binding Source={x:Reference ParentControl}, Path=PersonName}"/>
        </StackLayout>
    </ContentView.Content>
</ContentView>

然后我在另一個頁面中使用它。

 <StackLayout>
        <Entry x:Name="myEntry" Text="1"/>

        <local:PersonControl
             BindingContext="{x:Reference Name=myEntry}" 
              PersonName="{Binding Path=Text,  StringFormat='Welcome Mr {0}'}"
             ></local:PersonControl>
    </StackLayout>

因此,結果證明答案是在可綁定屬性簽名(在自定義控件的 cs 中)中傳遞默認值 object,如下所示:

public static readonly BindableProperty PersonProperty =
           BindableProperty.Create(
               nameof(Person),
               typeof(Person),
               typeof(PersonProperty ),
               default(Person)); //this was the fix

現在,我錯過了這個,因為我沒有意識到控件中的錯誤。 它只是沒有出現在 output window 中。任何人都可以指出如何正確全面調試 xamarin.forms 應用程序的方向嗎? 或者至少在將來發現這些錯誤? 謝謝

暫無
暫無

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

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