0

我想通过移动动画将汽车移动到点 0、0,但问题是我收到错误然后我使用此功能 - https://gyazo.com/1f47e18f955c12dc1907fcf4d7b19d6a。应用程序外观: https ://gyazo.com/10fb071534809229cb98955f9fb86231 。有我的代码(我在一篇WPF 文章中找到了这个函数。以编程方式将图像移动到 (X,Y) 的最简单方法?):

public void Eiti(Image target, double newX, double newY)
    {
        var top = Canvas.GetTop(target);
        var left = Canvas.GetLeft(target);
        TranslateTransform trans = new TranslateTransform();
        target.RenderTransform = trans;
        DoubleAnimation anim1 = new DoubleAnimation(top, newY - top, TimeSpan.FromSeconds(10));
        DoubleAnimation anim2 = new DoubleAnimation(left, newX - left, TimeSpan.FromSeconds(10));
        trans.BeginAnimation(TranslateTransform.XProperty, anim1);
        trans.BeginAnimation(TranslateTransform.YProperty, anim2);
    }

和 xml:

    <DockPanel Grid.Row="1" Background="#FF695887">
        <Canvas>
            <Image Name="car1" />
        </Canvas>
    </DockPanel>

如何解决?

4

1 回答 1

-1
public void Eiti(Image target, double newX, double newY)
    {
        var top = Canvas.GetTop(target);
        var left = Canvas.GetLeft(target);
        top = Double.IsNaN(top) ? 0 : top;
        left = Double.IsNaN(left) ? 0 : left;

        var storyboard = new Storyboard();

        DoubleAnimation anim1 = new DoubleAnimation(top, newY, TimeSpan.FromSeconds(10));
        Storyboard.SetTarget(anim1, target);
        Storyboard.SetTargetProperty(anim1, new PropertyPath(Canvas.TopProperty));
        storyboard.Children.Add(anim1);

        DoubleAnimation anim2 = new DoubleAnimation(left, newX, TimeSpan.FromSeconds(10));
        Storyboard.SetTarget(anim2, target);
        Storyboard.SetTargetProperty(anim2, new PropertyPath(Canvas.LeftProperty));
        storyboard.Children.Add(anim2);

        storyboard.Begin();
    }
于 2016-03-13T12:33:55.413 回答