Question

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.

Was it helpful?

Solution

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

OTHER TIPS

You would need to do myStory.Remove(this) before calling myStory.Begin(this) to have it start over from scratch. This is because calling Storyboard::Stop just stops the animation clocks, but leaves them intact. A subsequent call to Begin, will simply act as a resume. I agree that this is somewhat counterintuitive, however if you read the documentation ClockController::Stop, you will see the following in the remarks:

This method changes the target clock's CurrentState to Stopped.

A Stopped clock can be restarted by using the Begin, Seek, or SeekAlignedToLastTick method.

Sorry Scott, I wasn't paying attention. Did you try to set the FillBehavior on the Storyboard. Set the FillBehavior to Stop resets the animation. Not sure why Stop doesn't do it though...

<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>

 using System.Windows.Media.Animation;

then create new storyboard

 Storyboard storyboard_name = (Storyboard)(FindResource("overlay")); 
 storyboard_name.Begin();

the storyboard "storyboard_name" will start.

if you want to stop the storyboard.please try like this

storyboard_name.Stop();

if you want to remove the storyboard.please try like this

storyboard_name.Remove();

other details can be:

this.MyAnimation.FillBehavior = FillBehavior.Stop;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top