简体   繁体   中英

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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