我遇到了将对象属性绑定到标签的问题。在下面的示例中,将根据文档检索对象并将其设置为BindingContext。
问题是,在测试时,BusinessName标签会按预期显示值,但PostCode标签仍为空。为了测试该属性确实包含一个值,我已经设置了正确显示的第三个标签文本。
如果需要更多信息,请与我们联系。
public partial class AppointmentPage : ContentPage
{
public AppointmentPage(int calendarid)
{
InitializeComponent();
Detail d = JsonConvert.DeserializeObject<Detail>Core.GetDetail(calendarid).ToString());
BindingContext = d;
test.Text = d.PostCode;
}
}
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App.AppointmentPage">
<ContentPage.Content>
<ScrollView>
<StackLayout>
<Label Text="{Binding BusinessName}" />
<Label Text="{Binding PostCode}" />
<Label x:Name="test" Text="" />
</StackLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>
详情模型
class Detail
{
public int CalendarID { get; set; }
public int RecordID { get; set; }
public DateTime StartDateTime { get; set; }
public DateTime EndDateTime { get; set; }
public string BusinessName { get; set; }
public string Address { get; set; }
public string PostCode { get; set; }
public string WorkTel { get; set; }
public string Email { get; set; }
public string Status { get; set; }
public int RepID { get; set; }
}
更新
从列表中选择项目时,链接的页面将运行以下代码。在我将BindingContext设置为SelectedItem(包含BusinessName但不包含PostCode)并且这会覆盖页面加载上设置的内容时,会出现问题。
async void OnListItemSelected(object sender, SelectedItemChangedEventArgs e)
{
await Navigation.PushAsync(new AppointmentPage((e.SelectedItem as Diary).CalendarID)
{
BindingContext = e.SelectedItem as Diary
});
}