1

是否可以在代码隐藏中而不是 xaml 中以“老派”方式制作动画?

我只想要一个指向具有“反弹效果”的东西的箭头,我可以在自己的 for 循环中轻松做到这一点。但我不知道如何在循环内刷新或进行定时器延迟。我已经将图像放置在代码隐藏中的位置。我想做的就是这个简单的动画......

public void validationArrow()
{

    var validationArrow = new Image();
    validationArrow.Source = new BitmapImage(new Uri("/SlProject;component/arrow.png", UriKind.RelativeOrAbsolute));
    LayoutRoot.Children.Add(validationArrow);
    validationArrow.Stretch = Stretch.None;
    validationArrow.VerticalAlignment = System.Windows.VerticalAlignment.Top;
    validationArrow.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
    var arrowPosition = new TranslateTransform { X = 0, Y = 0 };
    validationArrow.RenderTransform = arrowPosition;

    validationArrow.Name = "validationArrow";

    for (int i = 150; i >= 0; i--)
    {
        arrowPosition.X = i;
        validationArrow.RenderTransform = arrowPosition;
        // how can I refresh screen and do some timing here?
    }
}
4

1 回答 1

3

没有像旧学校那样的学校;)在这里,这应该可以帮助您。您可以使用传递给 BuildEasing 方法的毫秒和 Y 平移值来更改“反弹”效果的速度和距离。

    private void RunStoryboard()
    {
        var arrowImage = new Image();
        arrowImage.RenderTransform = new CompositeTransform();
        arrowImage.Source = new BitmapImage(new Uri("/SlProject;component/arrow.png", UriKind.RelativeOrAbsolute));
        LayoutRoot.Children.Add(arrowImage);

        Storyboard storyboard = new Storyboard();
        storyboard.Children.Add(BuildKeyFrame(arrowImage));
        storyboard.Begin();
    }

    private DoubleAnimationUsingKeyFrames BuildKeyFrame(Image target)
    {
        DoubleAnimationUsingKeyFrames kf = new DoubleAnimationUsingKeyFrames();
        Storyboard.SetTarget(kf, target);
        Storyboard.SetTargetProperty(kf, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateY)"));

        kf.KeyFrames.Add(BuildEasing(100, 10));
        kf.KeyFrames.Add(BuildEasing(200, 0));
        kf.KeyFrames.Add(BuildEasing(300, 10));
        kf.KeyFrames.Add(BuildEasing(400, 0));
        kf.KeyFrames.Add(BuildEasing(500, 10));
        kf.KeyFrames.Add(BuildEasing(600, 0));

        return kf;
    }

    private EasingDoubleKeyFrame BuildEasing(int ms, int value)
    {
        return new EasingDoubleKeyFrame()
        {
            KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, ms)),
            Value = value
        };
    }
于 2012-04-12T02:52:18.823 回答