Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restarting a WPF Storyboard

Tags:

wpf

storyboard

What is the proper way to stop and restart a storyboard from .net code?

I'm trying ...

myStory.Stop(this);

Expecting that a subsequent call to .Begin(this); would restart from the timeline at zero, but instead, the storyboard picks up right where it was stopped.

I have tried

.Remove(this);

and I tried

.Seek(TimeSpan.Zero); 

which also didn't work.

More details ... Here is my storyboard sample.

<Storyboard x:Key="overlay">
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textone" Storyboard.TargetProperty="(UIElement.Opacity)">
        <SplineDoubleKeyFrame KeyTime="00:00:03.0" Value="0"/>
        <SplineDoubleKeyFrame KeyTime="00:00:03.0" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:06.0" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:06.0" Value="0"/>
    </DoubleAnimationUsingKeyFrames>
    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="texttwo" Storyboard.TargetProperty="(UIElement.Opacity)">
        <SplineDoubleKeyFrame KeyTime="00:00:07.0" Value="0"/>
        <SplineDoubleKeyFrame KeyTime="00:00:07.0" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:10.0" Value="1"/>
        <SplineDoubleKeyFrame KeyTime="00:00:10.0" Value="0"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

So the text in the textone runs, and if you close the screen, and return to the screen quickly, the texttwo, actually plays over a newly started storyboard. So the original (from the first screen) storyboard is still around and playing, even though I've removed, and stopped it.

like image 478
ScottCate Avatar asked May 07 '09 00:05

ScottCate


1 Answers

What about using Storyboard.Seek(TimeSpan.Zero)? Similar to seeking in a Stream, this should bring you back to the beginning of the animation.

I commented that you should also make sure that the IsControllable property is set to true. Keep that in mind!

Storyboard.Seek method

like image 196
Rob Avatar answered Nov 10 '22 15:11

Rob