Qi Tong
Qi Tong

Reputation: 89

How to hold animation when the animation is running in WPF?

For example, the translate transform animation is running, I want to hold on the animation, but not to stop it, how to do with that?

Upvotes: 3

Views: 248

Answers (1)

Anatoliy Nikolaev
Anatoliy Nikolaev

Reputation: 22702

In order to pause in animation worked correctly you need to call the Begin() method from the code, such as Loaded event handler (eg: Window_Loaded), because - quote from MSDN:

When you Begin a storyboard that was paused, it appears to resume and restart. However, that is not what actually happens. The Begin method actually replaces itself with an unpaused version. Each time the Begin method is called, clock objects are created for the storyboard. These clocks are distributed to the properties they animate. So, when the Begin method is called again, it does not restart its clocks; it replaces them with new clocks.

Example:

<Window.Resources>
    <Storyboard x:Key="SomeStoryboard">
        ...
    </Storyboard>
</Window.Resources>

In Loaded event or something else:

MyStoryboard = (Storyboard)this.FindResource("SomeStoryboard");
MyStoryboard.Begin();

And in Pause Button:

private void ButtonPause_Click(object sender, RoutedEventArgs e)
{
    MyStoryboard.Pause();
}

For more information, please see:

MSDN: Storyboard.Pause Method

Upvotes: 1

Related Questions