Forum Xamarin.Forms

Performant CollectionView datatemplates in Xamarin forms

philipzhengphilipzheng Member ✭✭
                        <CollectionView ItemsSource="{Binding Products}">
                            <CollectionView.ItemTemplate>
                                <DataTemplate>
                                    <datatemplates:ProductSummaryTemplate />
                                </DataTemplate>
                            </CollectionView.ItemTemplate>
                        </CollectionView>

When I try to put a ContentView inside of a CollectionView datatemplate, it seems to be rather slow compared to putting a ViewCell inside a ListView. I was wondering what is the recommended way to put a template inside a CollectionView? It seems like CollectionView does not allow you to put a ViewCell inside of it, but I read that ViewCell more performant than ContentView. My datatemplate looks like the below:

<ContentView ...>
    <Grid Padding="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="25" />
            <RowDefinition Height="25" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Image
            Grid.RowSpan="2"
            Aspect="AspectFit"
            HeightRequest="50"
            Source="{Binding thumbnail}" />
        <Label
            Grid.Column="1"
            FontAttributes="Bold"
            Text="{Binding title}" />
        <Label
            Grid.Row="1"
            Grid.Column="1"
            FontAttributes="Italic"
            Text="{Binding description}"
            VerticalOptions="End" />
    </Grid>
</ContentView>

Best Answer

Answers

  • igorkr_10igorkr_10 Member ✭✭✭✭
    edited May 4

    Why do you need ContentView? You can remove it and leave just Grid
    But I don't think that it will change something

  • igorkr_10igorkr_10 Member ✭✭✭✭
    edited May 4

    I have tested the ListView and CollectionView in my application.

    ListView video
    CollectionView video

    CollectionView:
    CollectionView

    ListView:
    ListView

    For me ListView is more performant

  • philipzhengphilipzheng Member ✭✭

    Thank you @jezh for directing me to that documentation. It's useful to know that we can change the image caching to cache for longer than the default 1 day (see https://montemagno.com/xamarinforms-image-caching-in-xaml/). Also thank you to @igorkr_10 for helping me see that, at least sometimes, ListView is more performant. I had experienced that as well in my own testing but wasn't sure if that was just an anomaly.
    I still need to wrap the datatemplate in a ContentView because it seems that I can't have a datatemplate with just a grid in its own file unless it's wrapped with a contentview.

Sign In or Register to comment.