WP7多重控制动画问题

时间:2010-12-08 06:56:04

标签: silverlight windows-phone-7

我在数组中存储了一组控件,我试图在循环中逐个动画所有控件,但我只能看到最后一个动画?

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

1 个答案:

答案 0 :(得分:1)

我无法解释这种行为。如果没有Dispatcher.BeginInvoke,您只会同时褪色所有项目。但是,在使用BeginInvoke时,我无法理解为什么你不会这样做。你所追求的仍然不是。您需要一个接一个地对动画进行排序。

执行此操作的最佳方法可能是使用具有多个动画的单个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;
    }

请注意额外的index参数,该参数用于指定动画何时开始。

现在你的代码很简单: -

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