c# - 如何在 MVVM 架构中将 animationview play 与 LottieForms 绑定(bind)?

标签 c# xaml xamarin xamarin.forms lottie

所以我在 ListView 中使用动画,我想随时播放一次,所以我想控制它。

这是图书馆 https://github.com/martijn00/LottieXamarin

我有一个类:

public class Info {
   public bool ReadMoreIconVisibility {get;set;}
}    

和xaml:

<forms:AnimationView Animation = "animationone.json" Loop = "false" IsVisible="{Binding ReadMoreIconVisibiliy}"/>

我可以成功地使用我的 xaml 来隐藏/不隐藏我的动画。但是我如何到达 AnimationView.Play() 方法,该方法只有在我将标签命名为 x:name 时才可用。

我如何利用 mvvm archictect 来播放我的动画?

我无法使用命令参数,因为它已被同一 ListView 中的另一个项目使用:

 <Button Command="{Binding Click}" CommandParameter="{x:Reference otherItemInListView}"/>

一个解决方案可能是用另一个对象扩展命令参数,如果是的话,那如何实现?不过,最好有另一种解决方案。

最佳答案

这是一个老问题,但我发布了这个答案,以防有人遇到同样的问题。

为了在不破坏 MVVM 模式的情况下将 Lottie 与 Xamarin Forms 结合使用,使用 ViewModel 中的绑定(bind)来启动和停止动画,您需要做的是创建两个触发 Action ,一个播放动画,一个重置进度到 0 然后暂停动画。或者您可以创建一个触发器来检查

然后在您的 ViewModel 中创建一个 bool 属性,当您想要开始导航时设置为 true,当您想要停止时设置为 false。在您的情况下,它是 ReadMoreIconVisibiliy

然后在您的 xaml 中使用触发器,请参见下面的代码。

<lottieForms:AnimationView
Animation="load_complete.json"
Speed="1"
AutoPlay="False">
    <lottieForms:AnimationView.Triggers>
        <MultiTrigger TargetType="lottieForms:AnimationView">
            <MultiTrigger.Conditions>
                <BindingCondition Binding="{Binding ReadMoreIconVisibiliy}" Value="True" />
            </MultiTrigger.Conditions>
            <MultiTrigger.EnterActions>
                <actions:StartLottieAnimationTriggerAction />
            </MultiTrigger.EnterActions>
            <MultiTrigger.ExitActions>
                <actions:StopLottieAnimationTriggerAction />
            </MultiTrigger.ExitActions>
        </MultiTrigger>
    </lottieForms:AnimationView.Triggers>
</lottieForms:AnimationView>

StartLottieAnimationTriggerAction代码

public class StartLottieAnimationTriggerAction : TriggerAction<AnimationView>
{
    protected override void Invoke(AnimationView sender)
    {
        sender.PlayAnimation();
    }
}

StopLottieAnimationTriggerAction代码

public class StopLottieAnimationTriggerAction : TriggerAction<AnimationView>
{
    protected override void Invoke(AnimationView sender)
    {
        sender.Progress = 0;
        sender.PauseAnimation();
    }
}

关于c# - 如何在 MVVM 架构中将 animationview play 与 LottieForms 绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52636061/

相关文章:

wpf - 如何修复 Visual Studio 2010 中的架构/命名空间错误? (WPF/XAML)

c# - 如何将字节数组转换为 FileImageSource?

c# - 无法启动 xxx.exe。先前对应用程序进行概要分析的尝试未成功完成。请重新启动应用程序

wpf - 如何在XAML中填充ComboBox

wpf - 有没有办法使用 Ruby 和 WPF?

android - Getview里面的Click方法被多次调用

xamarin - Xamarin 表单中的模态页面

c# - 更新数据 GridView

c# - FindFirstFile 与 IO.Directory.GetFiles

c# - 在 Visual Studio 2017 上添加对 Azure Function 的外部程序集引用