Animate Canvas.Left属性

Tim*_*Tim 0 silverlight animation

我正在尝试为画布中的图像的左侧属性设置动画.

当我在做的时候:

image.SetValue(Canvas.LeftProperty, destX[i]);
Run Code Online (Sandbox Code Playgroud)

这是有效的.但是当我在做的时候:

Animate(image, lastValue, destX[i], 500);
Run Code Online (Sandbox Code Playgroud)

    private void Animate(Image image, double val1, double val2, double miliseconds)
    {
        DoubleAnimation myDoubleAnimation = new DoubleAnimation { From = val1, To = val2, Duration = new Duration(TimeSpan.FromMilliseconds(miliseconds)) };

        TranslateTransform ts = new TranslateTransform();
        image.RenderTransform = ts;
        Storyboard.SetTarget(myDoubleAnimation, ts);

        Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(TranslateTransform.XProperty));
        Storyboard myMovementStoryboard = new Storyboard();
        myMovementStoryboard.Children.Add(myDoubleAnimation);
        myMovementStoryboard.Begin();
        myMovementStoryboard.Completed += (s, e) =>
            {
                image.SetValue(Canvas.LeftProperty, val2);
            };
    }
Run Code Online (Sandbox Code Playgroud)

它不起作用

我的Animate功能有什么问题?即使动画可能做得不好,完成的事件也应该将好的值重置为canvas.leftproperty.

但就我而言,出了点问题.

你将如何完成Animate功能?

在此先感谢您的帮助

dim*_*lou 6

我这样做了,它工作正常.

    DoubleAnimation myDoubleAnimation = new DoubleAnimation { From = val1, To = val2, Duration = new Duration(TimeSpan.FromMilliseconds(miliseconds)) };

    //set the target of the animation
    Storyboard.SetTarget(myDoubleAnimation, Image);
    //set the target property of the animation. Don't forget the ( ) around canvas.left
    Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath("(Canvas.Left)"));
    //create the storyboard
    Storyboard myMovementStoryboard = new Storyboard();
    //add the animation to the storyboard
    myMovementStoryboard.Children.Add(myDoubleAnimation);
    //begin animation
    myMovementStoryboard.Begin();
Run Code Online (Sandbox Code Playgroud)