简体   繁体   English

DoubleAnimation / Storyboard完成两次并重置动画值

[英]DoubleAnimation / Storyboard completes twice and resets animation value

While scaling a panel using a scale transform the application needs to reset the panel back to its original size. 使用比例转换对面板进行缩放时,应用程序需要将面板重置为其原始大小。 For this purpose a reset button starts a double animation that animates the scale transform from it's start value to 1 which means the panel will have it original value. 为此,一个重置按钮将启动一个双重动画,该动画将比例转换从其初始值动画化为1,这意味着面板将具有其原始值。

Visually the panel is scaled back to orignal size, but after the animation finishes the storyboard's complete event is raised twice, and once both of those events has been raised the value of the scale transform is set back to the value that it had before the animation. 从视觉上看,面板被缩放回原始大小,但是动画完成后,故事板的完成事件被引发两次,并且一旦这两个事件都被引发,缩放变换的值将被设置回动画之前的值。 。

private void ResetButton_Click(object sender, RoutedEventArgs e)
{
    if (!isReseting)
    {
        isReseting = true;

        this.doubleAnimation = new DoubleAnimation(1, new Duration(new TimeSpan(0,0,0, 1)), FillBehavior.Stop);
        this.resetStoryboard = new Storyboard();
        resetStoryboard.Children.Add(doubleAnimation);
        Storyboard.SetTarget(doubleAnimation, zoomSliderControl);
        Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(RangeBase.ValueProperty));

        resetStoryboard.RepeatBehavior = new RepeatBehavior(1);

        resetStoryboard.Completed += new EventHandler(ResetStoryboardCompleted);
        resetStoryboard.Begin();

    }
}

private void ResetStoryboardCompleted(object sender, EventArgs e)
{
    if (resetStoryboard != null)
    {
        //resetStoryboard.Stop(zoomSliderControl);
        //resetStoryboard.Remove(zoomSliderControl);
    }
    resetStoryboard = null;
    doubleAnimation = null;
    isReseting = false;

}

For example, if the value of the Slider control (named zoomSliderControl) is 1.5 before the animation, then it animates back to 1 as expected, but once the completed event of resetStoryBoard has been raised twice it is set back to 1.5 again. 例如,如果动画前Slider控件的值(名为zoomSliderControl)为1.5,则它会按预期动画设置为1,但是一旦两次完成resetStoryBoard的完成事件,它将再次设置为1.5。

I've tried debugging the application, and it's right after the second ResetStoryboardCompleted method has termined that the value is set to its original value so I'm guessing that I haven't configured the storyboard or animation correctly. 我已经尝试调试该应用程序,并且在第二个ResetStoryboardCompleted方法确定该值设置为其原始值之后,因此我猜测我没有正确配置情节提要或动画。

Appearently the default behaviour of storyboards is to revert to the original value once they have finished or (?) stopped. 显然,情节提要的默认行为是一旦完成 (?)停止,便恢复为原始值。 So the fix to this issue was to set the value of zoomSliderControl to the desired value upon storyboard completion. 因此,解决此问题的方法是在情节提要完成时将zoomSliderControl的值设置为所需值。

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

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