C# Canvas.GetTop()返回
我有一张画布,上面有一些元素。在我通过设置顶部和左侧属性的动画在画布上移动它们之后,偶尔对canvas.GetTop的后续调用会导致NaN 我没有正确地“关闭”动画吗 下面是我的动作C# Canvas.GetTop()返回,c#,wpf,C#,Wpf,我有一张画布,上面有一些元素。在我通过设置顶部和左侧属性的动画在画布上移动它们之后,偶尔对canvas.GetTop的后续调用会导致NaN 我没有正确地“关闭”动画吗 下面是我的动作 private void InternalMove(double durationMS, FrameworkElement fElement, Point point, EventHandler callback) { _moveElement = fElement; _destination = po
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);
}
谢谢我遇到过类似的情况(NaN),但情况不同。我记得,它与元素在容器中的位置有关
很抱歉,我无法提供更多帮助,但这可能会提供一些指导。似乎普遍的共识是
画布。如果没有明确设置值,GetTop(x)
将返回'Nan'(即使我明确设置了它,有时仍会得到该结果)
我现在使用的另一种方法是
Vector offset = VisualTreeHelper.GetOffset(fElement);
返回元素在其容器中的位置。在属性布局中检查xaml文件,确保“左”或“上”的值仅显示数字,而不是混合(自动)。
示例:Top 123(自动),这将导致Canvas.GetTop返回NaN。Oops意外地首先按下了向下箭头!现在回到我想要的,给一个正面的记号。