[英]Bindable Entry is not working inside custom control in 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.