如何将对象作为可绑定属性传递给Xamarin.Forms自定义控件

问题描述 投票:1回答:1

正如标题所示,我试图将Person对象传递给自定义控件,而不是分别传递每个属性。

所以这个:

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

代替此(基于this实现,它起作用)

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

我已经尝试在后面的PersonControl代码上更改可绑定属性签名,但是它不起作用。我实际上只是一个黑屏。

所以:1-这甚至可能(我知道它称为bindable property,但它是否也包含对象?和2-如果不是推荐的方法?

我想这样做的原因是person对象可能会随着时间的增长而增长,我宁愿只更新自定义控件,而不是使用页面及其视图模型。

更新:这是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模式在页面的视图模型上的属性。预先感谢您的帮助。

更新:Ive遵循此tutorial,并尝试用对象替换可绑定的字符串类型,但仍然不满意

c# mobile mvvm xamarin.forms user-controls
1个回答
0
投票

是,您可以使用Entry,键入一些文本,然后将文本传输到ContentView中的标签。这里正在运行GIF。

enter image description here

首先,我添加一个名为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布局的代码。请不要忘记在x:Name="ParentControl"标签中添加ContentView

<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>
© www.soinside.com 2019 - 2024. All rights reserved.