为 CarouselView 设置 DataTemplate

问题描述

嗨,我正在尝试在我的 viewmodel 中设置 CarouselView。我这里ItemTemplate的设置有问题。

设置

为了您的参考,我尝试定义 CarouselView carouselView

CarouselView carouselView = new CarouselView()
    {
               
        ItemsSource = items,IndicatorView = indicatorView,ItemTemplate = viewDataTemplate
    };

IndicatorView 正在工作:

private IndicatorView indicatorView = new IndicatorView()
    {
        IndicatorColor = Color.LightGray,IndicatorSize = 10,SelectedindicatorColor = Color.DarkGray,IndicatoRSShape = IndicatorShape.Circle,VerticalOptions = Layoutoptions.End,HorizontalOptions = Layoutoptions.Center,Margin = new Thickness(0,20)
    };

项目设置如下:

private ContentView[] CarouselItems()
        {
            ContentView[] items = new ContentView[5] {
                MapView(),SpeedView(),PaceView(),HeightView(),StepLengthView() };


            return items;

        }

        private ContentView StepLengthView()
        {
            ContentView view = new ContentView()
            {
                Content = new Label()
                {
                    Text = "Schrittlängenverlauf"
                }
            };

            return view;
        }

        private ContentView HeightView()
        {
            ContentView view = new ContentView()
            {
                Content = new Label()
                {
                    Text = "Höhenverlauf"
                }
            };

            return view;
        }

        private ContentView PaceView()
        {
            ContentView view = new ContentView()
            {
                Content = new Label()
                {
                    Text = "Balkendiagram: Pace"
                }
            };

            return view;
        }

        private ContentView SpeedView()
        {

            ContentView view = new ContentView()
            {
                Content = new Label()
                {
                    Text = "Geschwindigkeitsdiagram"
                }
            };

            return view;
        }

        private ContentView MapView()
        {
            polygon route = new polygon
            {
                strokeWidth = 8,strokeColor = Color.FromHex("#1BA1E2"),Geopath =
                {
                    new Position(47.6368678,-122.137305),new Position(47.6368894,-122.134655),new Position(47.6359424,new Position(47.6359496,-122.1325521),new Position(47.6424124,-122.1325199),new Position(47.642463,-122.1338932),new Position(47.6406414,-122.1344833),new Position(47.6384943,-122.1361248),new Position(47.6372943,-122.1376912)
                }
            };

            Map map = new Map()
            {
                HasScrollEnabled = false,HasZoomEnabled = false,};

            map.MapElements.Add(route);

            ContentView view = new ContentView()
            {
                Content = map
            };

            return view;
        }

我希望我的 ItemSources 是 ContentViews 的原因是因为我想在 CarouselView(地图、图像、图表等)中展示不同的想法。

错误的部分(编辑):

var viewDataTemplate = new DataTemplate(() =>
            {
                var grid = new Grid();

                var view = new ContentView();

                view.SetBinding(ContentView.contentproperty,"Content");

                grid.Children.Add(view);

                return new ViewCell { View = grid };
            });

问题

如果我尝试运行我得到的代码

system.invalidCastException: '指定的转换无效。'

我认为是DataTemplate的定义有问题:如果我像这样注释掉相应的行:

CarouselView carouselView = new CarouselView()
    {
               
        ItemsSource = items,//ItemTemplate = viewDataTemplate
    };

我没有收到错误消息,但是 CarouselView 当然没有正确显示视图(正如您在此处看到的:image)。

有没有人看到我的错误并且可以帮助我。

解决方法

来自CarouselView Microsoft docs

使用 CarouselView 时,切勿将 DataTemplate 对象的根元素设置为 ViewCell。这会导致抛出异常,因为 CarouselView 没有 cell 的概念。

您的 viewDataTemplate 不应返回 ViewCell

var viewDataTemplate = new DataTemplate(() =>
{
    var grid = new Grid();
    var view = new ContentView();
    view.SetBinding(ContentView.ContentProperty,"Content");
    grid.Children.Add(view);
    return grid;
});