tags:

views:

1531

answers:

3

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.

+2  A: 

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.

Sounds about right to me.
Mike Brown
this is still not working for me. after I both myStory.Remove(this) and myStory.Stop(this), if the scene comes back on the screen, before the storyboard has finished, the last (maybe better to call it Original) storyboard is still playing.
ScottCate
+4  A: 

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

Rob
Also, need to make sure that IsControllable is set to true and the appropriate containing object is marked.
Rob
Thank you! This is EXACTLY what I was after!!!!
ScottCate
A: 

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

Mike Brown