图像源更改时如何实现动画? [英] How to implement animation when Image source changed?

查看:73
本文介绍了图像源更改时如何实现动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理xamarin.forms,并且想在更改图像源时开始播放动画。

I am working on xamarin.forms and want to start an animation when the image source are changed.

           <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>

图像源绑定到ViewModel中的变量,如果用户单击图像和从服务器获得成功的响应。然后,将使用一些自定义动画来显示新图像,因此,由于图像源发生了更改,在哪里可以触发该动画?
我已经在线阅读了行为和触发器教程,似乎可以通过事件触发动画,但是Image类没有这样的 SourceChanged事件。

The image source is bind to an variable in the ViewModel and will be changed if user click the image and get a successful response from server. Then the new Image will be shown with some custom animation, so where can I trigger this animation since the image source changed? I have read the "Behavior" and "Trigger" tutorial online, It seems it could trigger an animation by an event, but Image class has not such an "SourceChanged" event.

推荐答案

您可以使用 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);
            });
    }
}

这篇关于图像源更改时如何实现动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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