我在程序启动后几秒钟创建了一些省略号,就像这样Ellipse myEllipse = new Ellipse();
然后我像这样移动省略号
public void MoveTo(Shape target, double newX, double newY, double oldX, double oldY)
{
TranslateTransform trans = new TranslateTransform();
target.RenderTransform = trans;
DoubleAnimation anim1 = new DoubleAnimation(0, newY - oldY, TimeSpan.FromSeconds(5));
DoubleAnimation anim2 = new DoubleAnimation(0, newX - oldX, TimeSpan.FromSeconds(5));
trans.BeginAnimation(TranslateTransform.YProperty, anim1);
trans.BeginAnimation(TranslateTransform.XProperty, anim2);
}
当他们移动到新位置时,我想在移动时获得新的X和Y位置,我使用计时器在我想要的时间获得他们的X和Y但是当我使用Canvas.GetLeft(name)
时它返回Nan并且我使用Point p = mEllipse.TranslatePoint(new Point(0, 0), canvasName);
并返回椭圆的初始位置而不是新的
答案 0 :(得分:1)
如果您为Canvas.GetLeft
和Canvas.GetTop
属性设置动画,则可以通过调用Canvas.Left
和Canvas.Top
来获取画布子项的当前位置:
public static void MoveTo(UIElement target, double x, double y)
{
var duration = TimeSpan.FromSeconds(5);
target.BeginAnimation(Canvas.LeftProperty, new DoubleAnimation(x, duration));
target.BeginAnimation(Canvas.TopProperty, new DoubleAnimation(y, duration));
}
请注意,您还必须为Canvas.Left
和Canvas.Top
属性设置初始值,例如
Canvas.SetLeft(myEllipse, 0);
Canvas.SetTop(myEllipse, 0);