【问题标题】:How to implement animation when Image source changed?图像源发生变化时如何实现动画?
【发布时间】:2017-10-13 21:33:35
【问题描述】:

我正在处理 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>

图像源绑定到 ViewModel 中的变量,如果用户单击图像并从服务器获得成功响应,图像源将被更改。然后新的图像将显示一些自定义动画,那么自从图像源更改后,我在哪里可以触发这个动画呢? 网上看了“行为”和“触发器”教程,貌似可以通过事件触发动画,但是Image类没有这样的“SourceChanged”事件。

【问题讨论】:

    标签: animation xamarin.forms


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多