HOW TO:移除腳本
更新:2007 年 11 月
下列範例示範如何在使用者巡覽離開包含 Storyboard 的頁面時,移除 Storyboard 使其停止繼續執行。
範例
<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://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>