2009-03-21 21 views
9

Tengo un lienzo con algunos UIElements activados. Después de moverlos al lienzo animando las propiedades superior e izquierda, muy ocasionalmente una llamada secundaria a Canvas.GetTop da como resultado NaN.Canvas.GetTop() devolviendo NaN

¿No estoy "cerrando" las animaciones correctamente?

Así es como yo estoy haciendo el movimiento

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

gracias

Respuesta

14

Parece que el consenso general es que Canvas.GetTop(x) devuelve 'Nan' si el valor no se establece explícitamente (incluso si lo configuro explícitamente, a veces obtengo ese resultado).

Un método alternativo Ahora estoy usando es

Vector offset = VisualTreeHelper.GetOffset(fElement); 

que devuelve la posición de fElement dentro de sus contenedores.

+0

¡Oops accidentalmente golpea primero la flecha hacia abajo! Ahora vuelvo a lo que pretendía dar un tic positivo. – Jim

1

me he encontrado con una situación similar (NaN), pero en un contexto diferente. Según recuerdo, tenía algo que ver con la forma en que se posicionó el elemento en el contenedor.

Lo siento, no pude proporcionar más ayuda, pero tal vez esto proporcionará alguna orientación.