如何:在腳本開始後使用事件觸發程式進行控制
此範例顯示如何在 Storyboard 開始之後對其進行控制。 若要使用 XAML 來開始 Storyboard,請使用 BeginStoryboard,以將動畫散發至其以動畫顯示的物件和屬性,然後開始分鏡腳本。 如果您指定 BeginStoryboard 名稱的方式是指定其 Name 屬性,則會使其成為可控制的分鏡腳本。 您接著可以在分鏡腳本開始之後,對其進行互動式控制。
搭配使用下列分鏡腳本動作與 EventTrigger 物件來控制分鏡腳本。
PauseStoryboard:暫停分鏡腳本。
ResumeStoryboard:繼續暫停的分鏡腳本。
SetStoryboardSpeedRatio:變更分鏡腳本速度。
SkipStoryboardToFill:將分鏡腳本推進到其填滿期間的結尾 (如果有的話)。
StopStoryboard:停止分鏡腳本。
RemoveStoryboard:移除分鏡腳本,以釋放資源。
範例
下列範例會使用可控制的分鏡腳本動作以互動方式控制分鏡腳本。
注意
若要查看使用程式碼來控制分鏡腳本的範例,請參閱在分鏡腳本開始後,使用其互動方法來進行控制。
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowTitle="Controlling a Storyboard" >
<StackPanel Margin="20" >
<!-- This rectangle is animated. -->
<Rectangle Name="myRectangle"
Width="100" Height="20" Margin="12,0,0,5" Fill="#AA3333FF" HorizontalAlignment="Left" />
<!-- This StackPanel contains all the Buttons. -->
<StackPanel Orientation="Horizontal" Margin="0,30,0,0">
<Button Name="BeginButton">Begin</Button>
<Button Name="PauseButton">Pause</Button>
<Button Name="ResumeButton">Resume</Button>
<Button Name="SeekButton">Seek</Button>
<Button Name="SkipToFillButton">Skip To Fill</Button>
<Button Name="SetSpeedRatioButton">Triple Speed</Button>
<Button Name="StopButton">Stop</Button>
<StackPanel.Triggers>
<!-- Begin the Storyboard -->
<EventTrigger RoutedEvent="Button.Click" SourceName="BeginButton">
<BeginStoryboard Name="MyBeginStoryboard">
<Storyboard >
<DoubleAnimation
Storyboard.TargetName="myRectangle"
Storyboard.TargetProperty="Width"
Duration="0:0:5" From="100" To="500" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<!-- Pause the Storyboard -->
<EventTrigger RoutedEvent="Button.Click" SourceName="PauseButton">
<PauseStoryboard BeginStoryboardName="MyBeginStoryboard" />
</EventTrigger>
<!-- Resume the Storyboard -->
<EventTrigger RoutedEvent="Button.Click" SourceName="ResumeButton">
<ResumeStoryboard BeginStoryboardName="MyBeginStoryboard" />
</EventTrigger>
<!-- Seek one second into the storyboard's active period. -->
<EventTrigger RoutedEvent="Button.Click" SourceName="SeekButton">
<SeekStoryboard
BeginStoryboardName="MyBeginStoryboard"
Offset="0:0:1" Origin="BeginTime" />
</EventTrigger>
<!-- Skip to Fill -->
<EventTrigger RoutedEvent="Button.Click" SourceName="SkipToFillButton">
<SkipStoryboardToFill BeginStoryboardName="MyBeginStoryboard" />
</EventTrigger>
<!-- Stop the Storyboard -->
<EventTrigger RoutedEvent="Button.Click" SourceName="StopButton">
<StopStoryboard BeginStoryboardName="MyBeginStoryboard" />
</EventTrigger>
<!-- Triple the speed of the Storyboard -->
<EventTrigger RoutedEvent="Button.Click" SourceName="SetSpeedRatioButton">
<SetStoryboardSpeedRatio SpeedRatio="3" BeginStoryboardName="MyBeginStoryboard" />
</EventTrigger>
</StackPanel.Triggers>
</StackPanel>
</StackPanel>
</Page>
如需其他範例,請參閱動畫範例資源庫。