2012-06-27 33 views
1

鑑於我的動畫scene1。如何使用名爲「跳過」的按鈕在後面使用C# code進行暫停,然後播放另一個動畫scene2如何使用後面的代碼暫停wpf動畫?

<Window.Resources> 
    <Storyboard x:Key="scene1"> 
     <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="whitebox"> 
      <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
      <EasingDoubleKeyFrame KeyTime="0:0:1.7" Value="0"/> 
     </DoubleAnimationUsingKeyFrames> 
     <StringAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" Storyboard.TargetName="charName"> 
      <DiscreteStringKeyFrame KeyTime="0:0:2" Value="Teacher"/> 
      <DiscreteStringKeyFrame KeyTime="0:0:7.8" Value="Teacher"/> 
      <DiscreteStringKeyFrame KeyTime="0:0:8" Value="Teacher"/> 
     </StringAnimationUsingKeyFrames> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.HorizontalAlignment)" Storyboard.TargetName="charName"> 
      <DiscreteObjectKeyFrame KeyTime="0:0:2" Value="{x:Static HorizontalAlignment.Left}"/> 
      <DiscreteObjectKeyFrame KeyTime="0:0:7.8" Value="{x:Static HorizontalAlignment.Left}"/> 
      <DiscreteObjectKeyFrame KeyTime="0:0:8" Value="{x:Static HorizontalAlignment.Left}"/> 
     </ObjectAnimationUsingKeyFrames> 
     <ObjectAnimationUsingKeyFrames 
     (too long… etc.) 

回答

1

除了Jeric保羅卡爾德隆的回答,還有暫停功能:storyBoard.Pause()
最主要的是,Pause()暫停在當前播放位置annimation,並Stop()不會停止,並返回到起點。

+0

謝謝..我想暫停它的當前位置..所以暫停是我需要的東西......與我回復傑裏克scene1時自動啓動時窗體加載...我想通過跳過按鈕使用代碼手動停止它,同時播放scene2 ...這可能嗎? – mitche027

+0

當然是!就像這樣調用場景2:Storyboard scene2 = this.Resources [「scene2」] as Storyboard;併發揮它。你可以按照你的喜好慢跑。 – HichemSeeSharp

+0

它真的幫了很多..但暫停後出現一個錯誤消息「對象引用未設置爲對象的實例。」該錯誤指出我的代碼爲暫停按鈕,它是:Storyboard scene1 = this.Resources [「scene_1」]作爲Storyboard; scene1.Pause(); – mitche027

2

在後面的代碼中,使用this.Resources [「scene1」]來獲取Storyboard對象。

Storyboard storyBoard = this.Resources["scene1"] as Storyboard; 
storyBoard.Begin(); 

用故事板 「已完成」 的事件時,故事板已經完成

storyBoard.Completed += eventHandler 

呼叫

storyBoard.Stop() 

停止動畫啓動另一個動畫。

+0

好吧,事情是scene1在表單加載時自動啓動...我想通過跳過按鈕後面的代碼手動停止它,並同時播放scene2 ...這可能嗎? – mitche027

+0

謝謝你的幫助:) – mitche027