Ele*_*ina 5 c# xaml xamarin xamarin.forms
我是Xamarin Forms和XAML的新手,我想<ListView>与A预约约会。我的XAML如下所示:
<ListView x:Name="appointmentsView"
ItemsSource="{Binding appointmentList}"
Grid.Row="1"
AbsoluteLayout.LayoutBounds="0.50, 0.2, 1, 0.5"
AbsoluteLayout.LayoutFlags="All">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Label TextColor="Black" Text="{Binding app1}"></Label>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
代码看起来像这样:
public partial class Home : ContentPage
{
public ObservableCollection<Appointment> appointmentList = new ObservableCollection<Appointment> ();
public Employee Tom { get; set; }
public Appointment app1 { get; set; }
public Home ()
{
InitializeComponent ();
Tom = new Employee("Tom", "Tommen");
app1 = new Appointment(new DateTime(), Tom);
appointmentList.Add(app1);
appointmentsView.ItemsSource = appointmentList;
}
Run Code Online (Sandbox Code Playgroud)
调试应用程序时,列表中没有任何内容,如果有人可以告诉我我做错了什么,我将不胜感激!
这实际上取决于Employee和 的Appointment定义方式。例如:
public class Appointment
{
public DateTime AppointmentDateTime { get; set; }
public Employee AppointmentEmployee { get; set; }
public Appointment(DateTime AppointmentDateTime, Employee AppointmentEmployee)
{
this.AppointmentDateTime = AppointmentDateTime;
this.AppointmentEmployee = AppointmentEmployee;
}
}
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Employee(string FirstName, string LastName)
{
this.FirstName = FirstName;
this.LastName = LastName;
}
override public string ToString()
{
return string.Format("{0}, {1}", LastName, FirstName);
}
}
Run Code Online (Sandbox Code Playgroud)
那么你的 XAML 应该是:
<ListView x:Name="appointmentsView"
ItemsSource="{Binding appointmentList}"
Grid.Row="1">
<ListView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding AppointmentEmployee}"></Label>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)