Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Animation on DataTemplate Items While loading in MAUI

Tags:

maui

I sometimes use CollectionView and BindableLayout to load a data set. I am unsure how to add animations to these items when loading data sets.

Let's say I have the following examples, How can I add animation (any random one / basic) while loading each item?

<CollectionView Grid.Row="1" ItemsSource="{Binding List} 
     ItemTemplate="{StaticResource ItemTemplate}">
     <CollectionView.ItemsLayout>
        <LinearItemsLayout Orientation="Vertical"  ItemSpacing="10"/>
     </CollectionView.ItemsLayout>
</CollectionView>

<HorizontalStackLayout Grid.Row="1" Spacing="10"
          BindableLayout.ItemsSource="{Binding List}" 
          BindableLayout.ItemTemplate="{StaticResource ItemTemplate}">
</HorizontalStackLayout>

Data Template

<DataTemplate x:Key="ItemTemplate">
    <VerticalStackLayout>
        <Label Text="{Binding Name}" />
        <Image Source="{Binding Path}" WidthRequest="50" HeightRequest="50"/>
    </VerticalStackLayout>
</DataTemplate>
like image 210
ARH Avatar asked Sep 03 '25 13:09

ARH


1 Answers

You can add animations using Behaviors and more specifically using AnimationBehavior from the Community Toolkits.

Currently there is only a FadeAnimation available but you can easily create new animations. Here is a simple example :

<CollectionView x:Name="Collection">
    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="local:SomeDataClass">
            <HorizontalStackLayout>
                <Label Text="{Binding SomeProp}" />
                <Label Text="Boup">
                    <Label.Behaviors>
                        <toolkit:AnimationBehavior EventName="Loaded">
                            <toolkit:AnimationBehavior.AnimationType>
                                <local:PaintTheRainbowAnimation Length="1000" />
                            </toolkit:AnimationBehavior.AnimationType>
                        </toolkit:AnimationBehavior>
                    </Label.Behaviors>
                </Label>
            </HorizontalStackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

Using this animation class :

class PaintTheRainbowAnimation : BaseAnimation
{
    public async override Task Animate(VisualElement view)
    {
        await view.BackgroundColorTo(Colors.Red);
        await view.BackgroundColorTo(Colors.Orange);
        await view.BackgroundColorTo(Colors.Yellow);
        await view.BackgroundColorTo(Colors.Green);
        await view.BackgroundColorTo(Colors.Blue);
        await view.BackgroundColorTo(Colors.Indigo);
        await view.BackgroundColorTo(Colors.Violet);
    }
}
like image 91
Poulpynator Avatar answered Sep 05 '25 03:09

Poulpynator