简体   繁体   English

如何在 MVVM 架构中将动画视图播放与 LottieForms 绑定?

[英]How to bind animationview play with LottieForms in a MVVM architecture?

So i work with a animation in a listview and i want to play it once whenever i want, so i want to control it.所以我在列表视图中使用 animation,我想随时随地播放一次,所以我想控制它。

This is the library https://github.com/martijn00/LottieXamarin这是图书馆https://github.com/martijn00/LottieXamarin

I have a class:我有一个 class:

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

And xaml:和 xaml:

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

I can successfully work with my xaml to hide/not hide my animation.我可以成功地使用我的 xaml 来隐藏/不隐藏我的 animation。 But how do i reach the AnimationView.Play() method, that is only available if i name my label x:name .但是我如何达到AnimationView.Play()方法,只有当我命名我的 label x:name时才可用。

How can i take advantage of the mvvm archictect in order to Play my animation?我如何利用 mvvm 架构师Play我的 animation?

I cannot work with the commandparameter because it is already used by another item in the same listview:我无法使用命令参数,因为它已被同一列表视图中的另一个项目使用:

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

One solution could be to extend the commandparameter with another object, if so how is that achievable?一种解决方案可能是用另一个 object 扩展命令参数,如果是这样,那如何实现? Preferably there is another solution to this though.不过,最好有另一种解决方案。

This is an old question but I am posting this answer in case anyone is facing the same problem.这是一个老问题,但我发布此答案以防万一有人面临同样的问题。

What you have to do in order to use Lottie with Xamarin Forms without breaking the MVVM pattern, using a Binding from the ViewModel to start and stop the animation, is creating two trigger actions, one that plays the animation and one that resets the progress to 0 then pauses the animation.为了在不破坏 MVVM 模式的情况下将 Lottie 与 Xamarin Forms 一起使用,您需要做的是使用 ViewModel 中的绑定来启动和停止动画,创建两个触发器动作,一个播放动画,另一个将进度重置为0 然后暂停动画。 Or you can create one trigger that check或者您可以创建一个触发器来检查

Then in your ViewModel create a bool property that is set to true when you want to start the navigation and false when you want to stop.然后在您的 ViewModel 中创建一个 bool 属性,该属性在您要开始导航时设置为 true,而在您要停止时设置为 false。 In your case it's ReadMoreIconVisibiliy .在您的情况下,它是ReadMoreIconVisibiliy

Then in your xaml consume the trigger, see code below.然后在您的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 Code StartLottieAnimationTriggerAction代码

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

StopLottieAnimationTriggerAction Code StopLottieAnimationTriggerAction代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM