简体   繁体   English

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

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

As the title suggests, I'm trying to pass in a Person object to a custom control instead of passing in each property separately.正如标题所示,我试图将一个 Person object 传递给自定义控件,而不是分别传递每个属性。

So this:所以这:

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

instead of this (which works, based on this implementation)而不是这个(有效,基于这个实现)

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

I've tried changing the bindable property signature on the PersonControl code behind but it's not working.我已经尝试更改后面的 PersonControl 代码上的可绑定属性签名,但它不起作用。 I actually just get a blank screen.我实际上只是得到一个空白屏幕。

So: 1 - Is this even possible (i know it's called a bindable property but does it take objects as well? and 2 - If not what is the recommended approach?所以:1 - 这甚至可能吗(我知道它被称为可绑定属性,但它是否也需要对象?2 - 如果不是,推荐的方法是什么?

The reason I want to do this is the person object may grow over time and I would rather just update the custom control instead of the consuming page AND it's view model.我想这样做的原因是这个人 object 可能会随着时间的推移而增长,我宁愿只更新自定义控件而不是消费页面及其视图 model。

Update: Here's the PersonControl Code:更新:这是 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();
        } 

    }

And here's the PersonControl xaml:这是 PersonControl xaml:

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

and lastly the consuming page:最后是消费页面:

 <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>

The person object is a property on the page's viewmodel as per mvvm pattern.根据 mvvm 模式,人 object 是页面视图模型上的一个属性。 Thanks in advance for your help.在此先感谢您的帮助。

Update: Ive followed this tutorial and tried to replace the bindable string type with an object but still no joy更新:我按照本教程尝试用 object 替换可绑定字符串类型,但仍然没有任何乐趣

Yes you can, I used a Entry , type some text, then transfer text to the Label in ContentView .是的,你可以,我使用了Entry ,键入一些文本,然后将文本传输到ContentView中的 Label。 Here is running GIF.这是运行 GIF。

在此处输入图像描述

First of all I add a property called PersonName like following code首先,我添加一个名为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); } 
        }
    }

Then, here is code about layout of ContentView .然后,这里是关于ContentView布局的代码。 Please do not forget to add x:Name="ParentControl" in ContentView tab.请不要忘记在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>

Then I use it in the other page.然后我在另一个页面中使用它。

 <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>

So, it turns out the answer was to pass a default object in the bindable property signature (in the custom control's cs) like so:因此,结果证明答案是在可绑定属性签名(在自定义控件的 cs 中)中传递默认值 object,如下所示:

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

Now, I missed this because I wasn't aware of errors in the control.现在,我错过了这个,因为我没有意识到控件中的错误。 It just wasn't showing up in the output window. Can anyone point me in the direction of how to comprehensively debug xamarin.forms apps properly?它只是没有出现在 output window 中。任何人都可以指出如何正确全面调试 xamarin.forms 应用程序的方向吗? Or at least catch these kind of errors in future?或者至少在将来发现这些错误? Thank you谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可绑定条目在 Xamarin.forms 的自定义控件中不起作用? - Bindable Entry is not working inside custom control in Xamarin.forms? 具有Bindable属性的自定义视图在Xamarin.Forms SAP上无法正确绑定 - Custom View with Bindable Property not binding properly on Xamarin.Forms SAP 如何为 Xamarin.Forms 中的自定义组件创建可绑定命令? - How to create bindable command for custom component in Xamarin.Forms? 定制控件的可绑定属性中的值未更新-Xamarin表格/棱镜 - Value is not updating in custom control bindable property - Xamarin forms / Prism Xamarin Forms DateTimePicker 自定义 ContentView 可绑定属性 - Xamarin Forms DateTimePicker custom ContentView bindable property 在 contenview Xamarin.forms 中为 ColumnDefinitionCollection 创建可绑定属性 - Create bindable property for ColumnDefinitionCollection in contenview Xamarin.forms 如何通过以 xamarin 形式绑定来使用可绑定属性? - How to use bindable property by Binding in xamarin forms? 如何在 Xamarin.Forms 中制作可绑定的标记扩展 - How to make a bindable markup extension in Xamarin.Forms Xamarin形成具有可绑定属性的ValueConverter - Xamarin Forms ValueConverter with Bindable Property 从零开始Xamarin.Forms中的自定义控件 - Custom Control in Xamarin.Forms from scratch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM