2011-12-04 11 views
2

我想在代碼中創建以下情節提要:如何通過代碼動態創建翻譯/移動故事板?

<Storyboard x:Name="m_activateIdentityStoryboard"> 
     <DoubleAnimationUsingKeyFrames 
      Storyboard.TargetProperty= 
        "(UIElement.RenderTransform).(CompositeTransform.TranslateY)" 
      Storyboard.TargetName="image"> 
      <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
      <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="-22"/> 
     </DoubleAnimationUsingKeyFrames> 
    </Storyboard> 

我曾嘗試以下:

Storyboard board = new Storyboard(); 
    Storyboard.SetTarget(board, view); 
    Storyboard.SetTargetProperty(board, 
     new PropertyPath(CompositeTransform.TranslateYProperty)); 

    DoubleAnimation upAnim = new DoubleAnimation() 
    { 
     Duration = new Duration(TimeSpan.FromMilliseconds(200)), 
     From = 0, 
     To = -22, 
     RepeatBehavior = new RepeatBehavior(1) 
    }; 
    board.Children.Add(upAnim); 

但不起任何作用。我很確定我指定了錯誤的PropertyPath,但是我不知道我應該把它放在什麼位置,甚至不知道我應該怎樣研究它。我也不明白「(UIElement.RenderTransform)。(CompositeTransform.TranslateY)」是什麼意思,以及如何將它轉換成c#。

謝謝! 豬

回答

4

的動畫正確的C#代碼應該是這樣的,

// initialize a new instance of the CompositeTransform which allows you 
    // apply multiple different transforms to your image 
    this.image.RenderTransform = new CompositeTransform(); 

    // create the timeline 
    var animation = new DoubleAnimationUsingKeyFrames(); 
    // add key frames to the timeline 
    animation.KeyFrames.Add(new EasingDoubleKeyFrame { KeyTime = TimeSpan.Zero, Value = 0 }); 
    animation.KeyFrames.Add(new EasingDoubleKeyFrame { KeyTime = TimeSpan.FromMilliseconds(200), Value = -22 }); 
    // notice the first parameter takes a timeline object not the storyboard itself 
    Storyboard.SetTargetProperty(animation, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateY)")); 
    Storyboard.SetTarget(animation, image); 

    // create the storyboard 
    var storyboard = new Storyboard() { RepeatBehavior = RepeatBehavior.Forever }; 
    // add the timeline to your storyboard 
    storyboard.Children.Add(animation); 

    // start the annimation 
    storyboard.Begin(); 

我已經把一些意見,希望他們對你有意義。 :)

+0

Rockin!非常感謝:) – swinefeaster

+0

現在我該如何防止圖像被剪切到它正在移動的面板的邊界? – swinefeaster