Canvas.GetTop()返回NaN [英] Canvas.GetTop() returning NaN
问题描述
我有一个带有一些UIElement的Canvas.在通过设置顶部和左侧属性的动画将它们移到画布上后,偶尔会调用Canvas.GetTop,结果为NaN.
I've got a Canvas with a few UIElements on. After I've moved them on the canvas by animating the top and left properties, very occasionally a subsiquent call to Canvas.GetTop results in NaN.
我不能正确地关闭"动画吗?
Am I not 'closing' the animations properly?
这就是我的举动
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);
}
谢谢
推荐答案
似乎普遍的共识是 Canvas.GetTop(x)
如果未明确设置值,则返回"Nan"(即使我确实做了明确设置,但有时还是会得到该结果.
It seems the general consensus is the Canvas.GetTop(x)
returns 'Nan' if the value is not explictly set (even tho I do explicitly set it I still sometimes get that result).
我现在正在使用的另一种方法是
An alternative method I'm now using is
Vector offset = VisualTreeHelper.GetOffset(fElement);
返回fElement在其容器中的位置.
which returns the position of fElement within it's container.
这篇关于Canvas.GetTop()返回NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!