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>
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With