보기 모델에 바인딩된 콘텐츠 페이지가 있습니다.
보기 모델에서 주소를 새로 만들고 일부 데이터를 제공합니다.
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] 삭제
몇 마디 만하겠습니다