ScalarKeyFrameAnimation 的持续时间变短了

Duration of ScalarKeyFrameAnimation got shorter

您好,我第一次尝试使用以下代码创建一个 "ScalarKeyFrameAnimation",当时我只切换开关。动画按照 'rotation' 动画对象配置的方式开始和停止,但是当尝试几次时,我注意到 Visual 旋转越来越慢,然后停止。喜欢 GIF

我已经在 Page_Loaded 事件上移动了旋转动画的创建。这样它只能创建一次。但之后什么都没有改变

    private Compositor compositor = Window.Current.Compositor;
    private Visual backvisual;        

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        backvisual = ElementCompositionPreview.GetElementVisual(FanIcon);

        backvisual.Size = new Vector2(100, 100);
        backvisual.CenterPoint = new Vector3(backvisual.Size / 2, 0);

        rotate = compositor.CreateScalarKeyFrameAnimation();
        rotate.InsertKeyFrame(1f, 360, compositor.CreateLinearEasingFunction());
        rotate.Duration = TimeSpan.FromMilliseconds(1000);
        rotate.IterationBehavior = AnimationIterationBehavior.Forever;

    }




    private void ToggleFanSec1_Toggled(object sender, RoutedEventArgs e)
    {
        if (ToggleFanSec1.IsOn == true)
        {
            backvisual.StartAnimation(nameof(Visual.RotationAngleInDegrees), rotate);
        }
        else
        {
            backvisual.StopAnimation(nameof(Visual.RotationAngleInDegrees));
        }
    }[![enter image description here][1]][1]

应该加一个初始状态的关键帧,没有初始状态很难定位,容易乱。您可以像下面这样更改代码。这意味着随着时间的推移,旋转动画从 0 到 360。

rotate = compositor.CreateScalarKeyFrameAnimation();
rotate.InsertKeyFrame(0f, 0);
rotate.InsertKeyFrame(1f, 360, compositor.CreateLinearEasingFunction());
rotate.Duration = TimeSpan.FromMilliseconds(4000);
rotate.IterationBehavior = AnimationIterationBehavior.Forever;