2016-02-27 61 views
1

我對C#/ WPF一般很新,所以很可能我錯過了一些東西。通過故事板WPF幀觸發器

有沒有辦法通過在Frame.Navigating事件上觸發的故事板來動畫Frame?大多數常見的我可以選擇,比如Frame.Loaded不是我之後的事件。

編輯:我不知道是一個自定義RoutedEvent最好的方法來處理這個?

當用戶單擊某個按鈕時,將調用Frame.Navigate方法並加載到新頁面中。

我想什麼發生的是......

  • 按鈕被點擊
  • (關鍵幀:0.1秒) - 幀100-0透明度
  • (關鍵幀消失:0.15秒) - 框架移開使用變換
  • (關鍵幀的畫布:0.2S) - 幀向後移動到屏幕上,不透明度回
  • 頁被加載100%
+0

看這裏:http://www.codeproject.com/Articles/364529/Animation-using-Storyboards-in-WPF – Fruchtzwerg

+0

在我看來,一個明顯的選擇是你的按鈕開始動畫而不是導航,並且根據動畫的狀態觸發導航。但這是一個非常模糊的輪廓;不可能確切知道你的情況下最適合哪種方式。沒有一個好的[mcve]顯示你已經嘗試過的,這個問題太廣泛了,不能在這裏合理地回答。 –

回答

0

這將爲你做。

<Frame x:Name="Frm" Width="499" Height="248" Content="Frame" Navigating="Frame_Navigating_1" BorderThickness="2" BorderBrush="#FF21BD9A"> 
    <Frame.RenderTransform> 
     <TranslateTransform x:Name="TransTrfm" /> 
    </Frame.RenderTransform> 
    <Frame.Resources> 
     <Storyboard x:Key="SbKey"> 
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity"> 
       <DoubleAnimationUsingKeyFrames.KeyFrames> 
        <LinearDoubleKeyFrame KeyTime="0:0:0" Value="1.0"/> 
        <LinearDoubleKeyFrame KeyTime="0:0:0.1" Value="0"/> 
       </DoubleAnimationUsingKeyFrames.KeyFrames> 
      </DoubleAnimationUsingKeyFrames> 

      <DoubleAnimationUsingKeyFrames BeginTime="0:0:0.15" Storyboard.TargetProperty="X" Storyboard.TargetName="TransTrfm"> 
       <DoubleAnimationUsingKeyFrames.KeyFrames> 
        <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0.0" /> 
        <LinearDoubleKeyFrame KeyTime="0:0:0.17" Value="-1000.0" /> 
        <LinearDoubleKeyFrame KeyTime="0:0:0.2" Value="0.0" /> 
       </DoubleAnimationUsingKeyFrames.KeyFrames> 
      </DoubleAnimationUsingKeyFrames> 

      <DoubleAnimationUsingKeyFrames BeginTime="0:0:0.35" Storyboard.TargetProperty="Opacity"> 
       <DoubleAnimationUsingKeyFrames.KeyFrames> 
        <LinearDoubleKeyFrame KeyTime="0:0:0" Value="1.0"/> 
       </DoubleAnimationUsingKeyFrames.KeyFrames> 
      </DoubleAnimationUsingKeyFrames> 

     </Storyboard> 
    </Frame.Resources> 
</Frame> 

代碼:

Storyboard sb; 
    private void Frame_Navigating(object sender, NavigatingCancelEventArgs e) 
    { 
     if (sb != null) 
     { 
      sb.Begin(); 
     } 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     sb = (Storyboard)Frm.Resources["SbKey"]; 
     Storyboard.SetTargetName(sb, "Frm"); 

     Frm.Navigate("Page1.xaml");   
    } 

您也可以嘗試使用ControlStoryboardAction行爲,並ChangePropertyAction行爲,避免代碼隱藏。