如何在Xamarin Carousel中将所有项目模板数据设置到单个视图中 [英] How to set all the item template data into a single view in Xamarin Carousel

查看:58
本文介绍了如何在Xamarin Carousel中将所有项目模板数据设置到单个视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将itemtemplate中的所有项目制作成单个视图,如下图所示,如何通过使用Xamarin CarouselView实现此目的,我正在使用这样的

  carousel = new CarouselView();carousel.BindingContext = this;carousel.ItemTemplate = itemTemplate;carousel.SetBinding(CarouselView.ItemsSourceProperty,new Binding(nameof(this.Items),mode:BindingMode.OneWay));LinearItemsLayout linearItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizo​​ntal);linearItemsLayout.SnapPointsAlignment = SnapPointsAlignment.Start;linearItemsLayout.SnapPointsType = SnapPointsType.Mandatory;carousel.ItemsLayout = linearItemsLayout;this.Children.Add(carousel,0,1); 

解决方案

最简单的方法是使用Horizo​​ntal

I have tried to make all the items in the itemtemplate into a single view as like in the below image, how to achieve this by using Xamarin CarouselView, i am using like this

        carousel = new CarouselView();
            carousel.BindingContext = this;
            carousel.ItemTemplate = itemTemplate;
            carousel.SetBinding(CarouselView.ItemsSourceProperty, new Binding(nameof(this.Items), mode: BindingMode.OneWay));
   LinearItemsLayout linearItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal);
            linearItemsLayout.SnapPointsAlignment = SnapPointsAlignment.Start;
            linearItemsLayout.SnapPointsType = SnapPointsType.Mandatory;
           carousel.ItemsLayout = linearItemsLayout;


 this.Children.Add(carousel,0,1);

Expected UI

解决方案

The easiest way of doing something like that would be to use the Horizontal CollectionView

CollectionView can display its items in a horizontal list by setting its ItemsLayout property to HorizontalList:

When you check the documents it gives a similar example

<CollectionView ItemsSource="{Binding Monkeys}"
            ItemsLayout="HorizontalList">
<CollectionView.ItemTemplate>
    <DataTemplate>
        <Grid Padding="10">
            <Grid.RowDefinitions>
                <RowDefinition Height="35" />
                <RowDefinition Height="35" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="70" />
                <ColumnDefinition Width="140" />
            </Grid.ColumnDefinitions>
            <Image Grid.RowSpan="2"
                   Source="{Binding ImageUrl}"
                   Aspect="AspectFill"
                   HeightRequest="60"
                   WidthRequest="60" />
            <Label Grid.Column="1"
                   Text="{Binding Name}"
                   FontAttributes="Bold"
                   LineBreakMode="TailTruncation" />
            <Label Grid.Row="1"
                   Grid.Column="1"
                   Text="{Binding Location}"
                   LineBreakMode="TailTruncation"
                   FontAttributes="Italic"
                   VerticalOptions="End" />
        </Grid>
    </DataTemplate>
</CollectionView.ItemTemplate>

Alternatively, this layout can also be accomplished by setting the ItemsLayout property to a LinearItemsLayout object, specifying the Horizontal ItemsLayoutOrientation enumeration member as the Orientation property value:

<CollectionView ItemsSource="{Binding Monkeys}">
<CollectionView.ItemsLayout>
    <LinearItemsLayout Orientation="Horizontal" />
</CollectionView.ItemsLayout>
...
</CollectionView>

This results in a single row list, which grows horizontally as new items are added:

这篇关于如何在Xamarin Carousel中将所有项目模板数据设置到单个视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆