2010-12-08 4 views
1

Je stocke un ensemble de contrôles dans un tableau et j'essaye d'animer tous les contrôles un par un dans une boucle mais je ne peux voir que le dernier en train d'animer?WP7 Problème d'animation de plusieurs contrôles

for (int i = 0; i < 4; i++) 
      { 
       Dispatcher.BeginInvoke(() => 
       { 
        var sb = new Storyboard(); 
        sb = CreateStoryboard(1.0, 0.0, this.Lights[0, i]); 
        sb.Begin(); 
       }); 
       } 


private Storyboard CreateStoryboard(double from, double to, DependencyObject targetControl) 
     { 
      Storyboard result = new Storyboard(); 

      DoubleAnimation animation = new DoubleAnimation(); 
      animation.From = from; 
      animation.To = to; 
      animation.Duration = TimeSpan.FromSeconds(1); 
      animation.BeginTime = TimeSpan.FromSeconds(1); 
      animation.AutoReverse = false; 
      Storyboard.SetTarget(animation, targetControl); 
      Storyboard.SetTargetProperty(animation, new PropertyPath(UIElement.OpacityProperty)); 

      result.Children.Add(animation); 
      return result; 
     } 
+0

Est-ce que ce changement d'approche signifie votre dernière question (http://stackoverflow.com/questions/4384159/wp7-ui-updating-issue) n'est pas plus valide? –

+0

Veuillez noter que Windows Phone 7 utilise une version de Silverlight 3, pas 4. –

Répondre

1

Je n'arrive pas à expliquer ce comportement. Sans le , vous obtiendriez que tous les éléments disparaissent en même temps. Cependant, je ne vois pas pourquoi vous n'obtiendriez pas la même chose en utilisant BeginInvoke. Cependant, ce n'est pas ce que vous recherchez. Vous devez séquencer les animations les unes après les autres.

Probablement la meilleure façon de le faire est d'utiliser un seul StoryBoard avec de multiples animations, le séquençage des animations est le afterall point entier d'un Storyboard.

private DoubleAnimation CreateAnimation(double from, double to, DependencyObject targetControl, int index) 
    { 
     DoubleAnimation animation = new DoubleAnimation(); 
     animation.From = from; 
     animation.To = to; 
     animation.Duration = TimeSpan.FromSeconds(1); 
     animation.BeginTime = TimeSpan.FromSeconds(1 * index); 
     animation.AutoReverse = false; 
     Storyboard.SetTarget(animation, targetControl); 
     Storyboard.SetTargetProperty(animation, new PropertyPath(UIElement.OpacityProperty)); 

     return animation; 
    } 

Notez le paramètre index supplémentaire et est utilisé pour spécifier quand l'animation doit commencer. Maintenant, votre code

est simplement: -

var sb = new Storyboard();  

for (int i = 0; i < 4; i++)  
{  
    sb.Children.Add(CreateAnimation(1.0, 0.0, this.Lights[0, i], i);  
} 

sb.Begin();