2009-03-21 38 views
9

Tôi có Canvas với một vài UIElements. Sau khi tôi đã di chuyển chúng trên canvas bằng cách làm động các thuộc tính trên cùng và bên trái, rất thỉnh thoảng một cuộc gọi độc lập tới kết quả Canvas.GetTop trong NaN.Canvas.GetTop() trả về NaN

Tôi không 'đóng' các hoạt ảnh đúng cách?

Đây là cách tôi đang làm di chuyển

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); 
} 

nhờ

Trả lời

14

Có vẻ như sự đồng thuận chung là Canvas.GetTop(x) trả về 'Nan' nếu giá trị không được thiết lập rõ ràng (thậm chí tho tôi đặt rõ ràng rằng đôi khi tôi vẫn nhận được kết quả đó).

Một phương pháp thay thế bây giờ tôi đang sử dụng là

Vector offset = VisualTreeHelper.GetOffset(fElement); 

mà trả về vị trí của fElement trong nó chứa.

+0

Rất tiếc, số lần vô tình nhấn vào mũi tên xuống trước! Bây giờ trở lại với những gì tôi dự định cho một dấu tích cực. – Jim

1

Tôi đã chạy vào một tình huống tương tự (NaN), nhưng trong một bối cảnh khác nhau. Như tôi nhớ lại, nó có liên quan đến yếu tố được định vị trong container như thế nào.

Rất tiếc, tôi không thể cung cấp thêm trợ giúp, nhưng có thể điều này sẽ cung cấp một số hướng dẫn.

Các vấn đề liên quan