Невозможно привязать свойства объекта к пользовательскому элементу управления - PullRequest
0 голосов
/ 11 апреля 2019

У меня есть страница содержимого, связанная с моделью представления.

В представлении модели я создаю новый адрес и даю ему некоторые данные

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>

При запуске я ожидал увидеть свойства адреса, но вместо этого все они были бы пустыми, за исключением жестко закодированного текста

Picture of the app running

Я добавил кнопку в элемент управления, чтобы помочь с отладкой, если я остановил событие Button_OnClicked, я могу видеть свойства адреса, и он имеет правильные данные, но он просто не показывает его в списке, что я делаю неправильно?

Address properties

1 Ответ

0 голосов
/ 11 апреля 2019

Благодаря подсказкам Джейсона я удалил следующие строки из конструктора 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));

Спасибо за помощь!

...