我有一个绑定到视图模型的内容页面。
在视图模型中,我新建一个地址并给它一些数据
Address = new Address
{
UserId = Guid.NewGuid(),
PrimaryAddress = true,
Postcode = "G23 5HU",
Street = "Smith Street",
City = "Large City",
PhoneNumber = "115151"
};
我在内容页面上也有一个 AddressInfo 控件,就像这样
<controls:AddressInfo Address="{Binding Address}"></controls:AddressInfo>
这是它自己的控制
public partial class AddressInfo : ContentView
{
public static readonly BindableProperty AddressProperty =
BindableProperty.Create(nameof(Address), typeof(Address), typeof(AddressInfo), null);
public Address Address
{
get { return (Address)GetValue(AddressProperty); }
set { SetValue(AddressProperty, value); }
}
public AddressInfo()
{
InitializeComponent();
Street.SetBinding(Label.TextProperty, new Binding(nameof(Address.Street), source: this));
City.SetBinding(Label.TextProperty, new Binding(nameof(Address), source: this));
PhoneNumber.SetBinding(Label.TextProperty, new Binding(nameof(Address), source: this));
PostCode.SetBinding(Label.TextProperty, new Binding(nameof(Address), source: this));
}
这是控件 XAML
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TyreKlicker.XF.Core.Controls.AddressInfo">
<ContentView.Content>
<StackLayout>
<Label
x:Name="Street"
Text="{Binding Address.Street}" />
<Label
x:Name="City"
Text="{Binding Address.City}" />
<Label Text="Hardcoded Text" ></Label>
<Label
x:Name="PostCode"
Text="{Binding Address.Postcode}" />
<Label
x:Name="PhoneNumber"
Text="{Binding Address.PhoneNumber}" />
<Label x:Name="CardHeader"
Text="{Binding Header}"
TextColor="{StaticResource PrimaryDark}"
FontSize="Large" Margin="10,0,0,0" />
<Button Clicked="Button_OnClicked"></Button>
</StackLayout>
</ContentView.Content>
当我运行它时,我希望看到地址属性,但除了硬编码文本外,它全部为空白
我向控件添加了一个按钮以帮助调试,如果我停止 Button_OnClicked 事件,我可以看到地址的属性并且它具有正确的数据,但它只是没有在列表中显示它我做错了什么?
感谢 Jason 的提示,我从 AddressInfo 构造函数和中提琴中删除了以下几行,它起作用了
//Street.SetBinding(Label.TextProperty, new Binding(nameof(Address.Street), source: this));
//City.SetBinding(Label.TextProperty, new Binding(nameof(Address), source: this));
//PhoneNumber.SetBinding(Label.TextProperty, new Binding(nameof(Address), source: this));
//PostCode.SetBinding(Label.TextProperty, new Binding(nameof(Address), source: this));
谢谢您的帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句