Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace: http://schemas.microsoft.com/winfx/2006/xaml/presentation
Pause, Resume, Stop, Remove, or Otherwise Control a Storyboard Interactively
To be able to pause, resume, remove, or otherwise control a Storyboard interactively, set the Name property of its BeginStoryboard and use a ControllableStoryboardAction object (such as PauseStoryboard, ResumeStoryboard, or StopStoryboard) to control it by referencing its Name. If the Name of BeginStoryboard is unspecified, the Storyboard cannot be interactively controlled after it is begun. See How to: Control an Animation After it has Started for more information.
When to Remove Storyboards
You should use the RemoveStoryboard action to remove any storyboards begun with a HandoffBehavior of Compose. It's also a good idea to remove background animations when the object they animate is no longer being used. For example, if you apply an indefinitely repeating storyboard using an element's Loaded event, you should also create an EventTrigger that uses a RemoveStoryboard action to remove the storyboard on the element's Unloaded event.
The following example shows how to remove a Storyboard to stop it from continuing when the user navigates away from the page containing the Storyboard.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="RootPage"> <StackPanel> <!-- The MediaElement control plays the sound. --> <MediaElement Name="myMediaElement" /> <Button Name="BeginButton">Begin</Button> <StackPanel.Triggers> <!-- This trigger causes the Storyboard to start (the music start) when you click the "BeginButton" button. --> <EventTrigger RoutedEvent="Button.Click" SourceName="BeginButton"> <EventTrigger.Actions> <BeginStoryboard Name="myBeginStoryboard"> <Storyboard> <MediaTimeline Source="C:\WINDOWS\Media\town.mid" Storyboard.TargetName="myMediaElement" RepeatBehavior="Forever" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> <!-- When this page unloads, like when you navigate away from the page, this trigger causes the Storyboard to be removed and the clock to stop. If you come back to this page and click the button to start the Storyboard, the music starts from the beginning. Alternatively, if you don't unload the Storyboard when you leave the page, the Storyboard does not stop while you're gone and when you navigate back to the page you hear the music continuing as though you had never left. --> <EventTrigger RoutedEvent="Page.Unloaded" > <EventTrigger.Actions> <RemoveStoryboard BeginStoryboardName="myBeginStoryboard" /> </EventTrigger.Actions> </EventTrigger> </StackPanel.Triggers> </StackPanel> </Page>
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.