【问题标题】:C# WinUI project, How to add Multiple ExpressionAnimation on same control?C# WinUI 项目,如何在同一个控件上添加多个 ExpressionAnimation?
【发布时间】:2021-12-24 23:56:41
【问题描述】:

我开始研究一些特定按钮的动画,基本上我现在正在做的事情是我正在努力寻找为同一控件插入多个表情动画的正确方法。

如果我只调用一次 button1.StartAnimation("some expression animation reference"),可以按预期工作,但是一旦我尝试启动第二个动画 button1.StartAnimation("a second expression animation reference") 程序就会崩溃立即出现错误 System.ArgumentException: 'Value does not fall in the expected range.'

我试图做的是对同一个控件使用多个表达式动画来同时更新不同的属性。

我的问题很简单,如何在同一个控件上同时执行多个表达式动画,或者如何让表达式动画运行多个表达式并更新多个目标属性

这是我试图执行的代码:

        ExpressionAnimation anim1 = _compositor.CreateExpressionAnimation();
        anim1.Expression = "-((self.Scale.X - 1) * (self.ActualSize.X * 0.01) * 50)";
        anim1.Target = "Translation.X";

        ExpressionAnimation anim2 = _compositor.CreateExpressionAnimation();
        anim2.Expression = "-((self.Scale.Y - 1) * (self.ActualSize.Y * 0.01) * 50)";
        anim2.Target = "Translation.Y";

        
        anim1.SetExpressionReferenceParameter("self", button1);
        anim2.SetExpressionReferenceParameter("self", button1);
        

        button1.StartAnimation(anim1);//adds just fine and works as intended
        button1.StartAnimation(anim2);//crashes instantly with error System.ArgumentException: 'Value does not fall within the expected range.'

PS:我知道我可能可以使用向量作为位置并使用单个表达式,但我真正想要的是了解如何使用更多表达式来更新同一控件的多个目标值。

提前感谢您的帮助!

【问题讨论】:

    标签: c# uwp winui-3 visual-studio-2022


    【解决方案1】:

    如何在同一个控件上同时执行多个表情动画,

    恐怕你不能像上面那样执行多个动画,这会导致第二个动画无法使用第一个动画使用的控件。

    对于执行多个表达式,我们建议您使用+combine together

    例如

    _parallaxExpression = compositor.CreateExpressionAnimation(
     "(ScrollManipulation.Translation.Y + StartOffset) * ParallaxValue - " +
    "(ScrollManipulation.Translation.Y + StartOffset)");
    

    【讨论】:

    • 您好,谢谢您的回复,我对这种方法有几个问题,在我看来,您将 2 个字符串合并为 1 个,那么它如何分成 2 个表达式? isnt ("some string" + "another string") 仍然只有 1 个字符串?合成器如何知道在哪里拆分 2 个表达式?其次,我们如何更新让我们说 2 个不同的目标变量(每个表达式 1 个)?我真的很好奇这个解决方案。提前感谢您的回复!
    • 我不得不说。对于您的情况,更好的方法是使用向量来接近您上面提到的。
    猜你喜欢
    • 2019-12-01
    • 2023-02-25
    • 2021-09-05
    • 2017-03-08
    • 2023-03-17
    • 1970-01-01
    • 2018-05-05
    • 2021-11-28
    • 1970-01-01
    相关资源
    最近更新 更多