如何在图像源改变时实现动画?
问题描述:
我正在使用xamarin.forms,并希望在图像源发生更改时启动动画。如何在图像源改变时实现动画?
<ListView >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Image Source="{Binding FavoriteImage}" x:Name="favoriteImage">
<Image.GestureRecognizers>
TapGestureRecognizer Command="{Binding Source={x:Reference CurrentPage},Path=BindingContext.ClickLikedCommand}" CommandParameter="{Binding .}" NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
图像源被绑定到视图模型的变量,并且如果用户单击图像并从服务器获取一个成功的响应将被改变。然后新的图像将显示一些自定义动画,所以我可以在图像源改变后触发这个动画? 我已经在线阅读了“行为”和“触发器”教程,看起来它可以通过事件触发动画,但Image类没有这样的“SourceChanged”事件。
答
可以使用OnPropertyChanged
方法:
public class ExImage : Image
{
protected override void OnPropertyChanged(string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (propertyName == nameof(Source))
Device.BeginInvokeOnMainThread(async() =>
{
await this.ScaleTo(1.2);
await this.ScaleTo(1);
});
}
}