Canvas.GetTop()返回NaN

Pau*_*ulB 9 c# wpf

我有一个带有一些UIElements的Canvas.通过动画顶部和左侧属性将我们在画布上移动后,偶尔会调用Canvas.GetTop,导致NaN.

我没有正确地"关闭"动画吗?

这是我正在采取行动的方式

private void InternalMove(double durationMS, FrameworkElement fElement, Point point, EventHandler callback)
{
   _moveElement = fElement;
   _destination = point;

   Duration duration = new Duration(TimeSpan.FromMilliseconds(durationMS));

   DoubleAnimation moveLeftAnimation = new DoubleAnimation(Canvas.GetLeft(fElement), point.X, duration, FillBehavior.Stop);
   Storyboard.SetTargetProperty(moveLeftAnimation, new PropertyPath("(Canvas.Left)"));

   DoubleAnimation moveTopAnimation = new DoubleAnimation(Canvas.GetTop(fElement), point.Y, duration, FillBehavior.Stop);
   Storyboard.SetTargetProperty(moveTopAnimation, new PropertyPath("(Canvas.Top)"));

   // Create a storyboard to contain the animation.
   _moveStoryboard = new Storyboard();
   if (callback != null) _moveStoryboard.Completed += callback;

   _moveStoryboard.Completed += new EventHandler(s1_Completed);
   _moveStoryboard.Children.Add(moveLeftAnimation);
   _moveStoryboard.Children.Add(moveTopAnimation);
   _moveStoryboard.FillBehavior = FillBehavior.Stop;
   _moveStoryboard.Begin(fElement);
}

private void s1_Completed(object sender, EventArgs e)
{
    if (_moveStoryboard != null)
    {
       _moveStoryboard.BeginAnimation(Canvas.LeftProperty, null, HandoffBehavior.Compose);
       _moveStoryboard.BeginAnimation(Canvas.TopProperty, null, HandoffBehavior.Compose);
    }

    Canvas.SetLeft(_moveElement, _destination.X);
    Canvas.SetTop(_moveElement, _destination.Y);
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Pau*_*ulB 15

似乎普遍的共识是Canvas.GetTop(x)如果没有明确地设定值,则返回'Nan'(即使我明确地设定它,我仍然有时得到那个结果).

我现在使用的另一种方法是

Vector offset = VisualTreeHelper.GetOffset(fElement);
Run Code Online (Sandbox Code Playgroud)

它返回fElement在其容器中的位置.