0

我有一个绑定到视图模型的内容页面。

在视图模型中,我新建了一个地址并给它一些数据

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 事件,我可以看到地址的属性并且它具有正确的数据,但它只是没有在列表中显示它我做错了什么?

地址属性

4

1 回答 1

0

感谢 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));

谢谢您的帮助!

于 2019-04-11T14:45:26.763 回答