如何通过代码动态创建翻译/移动故事板?

时间:2011-12-04 21:19:07

标签: silverlight windows-phone-7.1 windows-phone-7

我正在尝试在代码中创建以下故事板:

<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#。

谢谢! 猪

1 个答案:

答案 0 :(得分: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();

我已经发表了一些评论,希望它们对你有意义。 :)

相关问题