开发者

DoubleAnimation / Storyboard completes twice and resets animation value

开发者 https://www.devze.com 2022-12-12 03:25 出处:网络
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 tran

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 Propert开发者_开发技巧yPath(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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号