【问题标题】:Carousel View and List View binding not working with Xamarin Forms轮播视图和列表视图绑定不适用于 Xamarin 表单
【发布时间】: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


    【解决方案1】:

    尝试添加

    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的内容设置为轮播即可。

    【讨论】:

    • 你救了我的理智!我不敢相信,在我试图让它发挥作用之后,我没有想到这样的解决方案!非常感谢!
    • @AndreaBoscaro 很高兴它可以帮助你:)
    • 我收到错误消息。像值不能为空。参数名称:key
    猜你喜欢
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    相关资源
    最近更新 更多