【发布时间】:2018-01-18 17:37:41
【问题描述】:
我对 Xamarin Forms 有一个大问题 - 我必须在不同页面中创建一个带有不同列表的轮播视图/页面。这是我写的代码
我的主要内容页面是 DailyCarouselPage
public class DailyCarouselPage : ContentPage
{
public CarouselViewControl carousel;
public DailyCarouselPage ()
{
ObservableCollection<DailyAppointmentList> collection = new ObservableCollection<DailyAppointmentList>();
collection.Add(new DailyAppointmentList());
collection.Add(new DailyAppointmentList());
StackLayout body = new StackLayout();
carousel = new CarouselViewControl();
carousel.ItemsSource = collection;
carousel.ItemTemplate = new DailyAppointmentListTemplate();
body.Children.Add(carousel);
Content = body;
}
}
然后我有一个包含我必须显示的列表列表的类:
class DailyAppointmentList
{
public ObservableCollection<Appointment> list { get; set; }
public DailyAppointmentList()
{
list = new ObservableCollection<Appointment>();
list = new AppointmentListModel().List; //this list is declared in another class, which isn't important to the problem
}
}
基本ListView模板:
public class DailyAppointmentListTemplate : DataTemplate
{
public DailyAppointmentListTemplate ()
{
StackLayout stack = new StackLayout();
ListView list = new ListView()
{
ItemTemplate = new DataTemplate(typeof(AppointmentCellTemplate))
};
list.SetBinding(ListView.ItemsSourceProperty, "list");
stack.Children.Add(list);
}
}
列表单元格的模板:
public class AppointmentCellTemplate : ViewCell
{
public AppointmentCellTemplate ()
{
StackLayout stack = new StackLayout();
Label subject = new Label();
subject.SetBinding(Label.TextProperty, "Subject");
Label description = new Label();
description.SetBinding(Label.TextProperty, "Description");
Label date = new Label();
date.SetBinding(Label.TextProperty, "Start");
stack.Children.Add(subject);
stack.Children.Add(description);
stack.Children.Add(date);
View = stack;
}
}
最后,我的Appointment 班级:
public class Appointment
{
public Guid? AppointmentId { get; set; }
public DateTime? Start { get; set; }
public DateTime? End { get; set; }
public string Subject { get; set; }
public string Description { get; set; }
public Costumer Costumer { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
}
无论如何,我都无法在主DailyCarouselPage 中显示该列表。我尝试了其他方法,但也没有奏效。好像我一直在使用绑定做某种错误,但我无法弄清楚我做错了什么。
【问题讨论】:
标签: c# xamarin xamarin.forms xamarin.ios xamarin.android