我对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
中。我尝试了其他方法,但也没有用。好像我一直在用绑定做一些错误,但我无法弄清楚我做错了什么。
答案 0 :(得分:0)
尝试添加
Xamarin.Forms.Init();
CarouselViewRenderer.Init();
在您的iOS和Android项目中展示它。
然后在我的测试中,如果您要在CarouselViewControl
中添加ListView,请将ItemSource
设置为:
public DailyCarouselPage()
{
ObservableCollection<ListView> collection = new ObservableCollection<ListView>();
ListView myListView = new ListView
{
ItemTemplate = new DataTemplate(typeof(AppointmentCellTemplate)),
ItemsSource = (new DailyAppointmentList()).list
};
collection.Add(myListView);
collection.Add(myListView);
carousel = new CarouselViewControl();
carousel.ItemsSource = collection;
Content = carousel;
}
此外,请注意:只需将ContentPage的内容设置为轮播。