【问题标题】:WP7 Multiple controls animation problemWP7 多控件动画问题
【发布时间】:2012-07-21 14:19:51
【问题描述】:

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

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

【问题讨论】:

标签: silverlight windows-phone-7


【解决方案1】:

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

可能最好的方法是使用单个 StoryBoard 和多个动画,动画的排序毕竟是 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();     

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    相关资源
    最近更新 更多