故事板中的多个动画同时出现?

时间:2010-09-22 21:37:12

标签: c# .net wpf visual-studio

我创建了一些ColorAnimations并希望它们同时运行(无论它们是否同步运行都无关紧要)。 可悲的是,其中只有一个跑了。

       storyboard = new Storyboard();

        //Animation Auditorium
        ColorAnimation SpotLightAnimation = new ColorAnimation();
        SpotLightAnimation.To = Color.FromArgb(1, Convert.ToByte(random.Next(0, 255)), Convert.ToByte(random.Next(0, 255)), Convert.ToByte(random.Next(0, 255)));
        SpotLightAnimation.Duration = TimeSpan.FromSeconds(3);
        SpotLightAnimation.Completed += new EventHandler(storyboard_Completed);

        this.RegisterName("MySpotlight", karte.SpotLightAuditorium);
        Storyboard.SetTargetName(SpotLightAnimation, "MySpotlight");
        Storyboard.SetTargetProperty(SpotLightAnimation, new PropertyPath(SpotLight.ColorProperty));

        storyboard.Children.Add(SpotLightAnimation);


        //Animation Wohnzimmer
        ColorAnimation SpotLightWohnzimmerAnimation = new ColorAnimation();
        SpotLightWohnzimmerAnimation.To = Color.FromArgb(1, Convert.ToByte(random.Next(0, 255)), Convert.ToByte(random.Next(0, 255)), Convert.ToByte(random.Next(0, 255)));
        SpotLightWohnzimmerAnimation.Duration = TimeSpan.FromSeconds(3);
        SpotLightAnimation.Completed += new EventHandler(storyboard_Completed);

        this.RegisterName("MySpotLightWonzimmer", karte.SpotLightWohnzimmer);
        Storyboard.SetTargetName(SpotLightWohnzimmerAnimation, "MySpotLightWonzimmer");
        Storyboard.SetTargetProperty(SpotLightWohnzimmerAnimation, new PropertyPath(SpotLight.ColorProperty));

        storyboard.Children.Add(SpotLightWohnzimmerAnimation);

        storyboard.Begin(this);


    }

    void storyboard_Completed(object sender, EventArgs e)
    {
        (storyboard.Children[0] as ColorAnimation).To = Color.FromArgb(1, Convert.ToByte(random.Next(0, 255)), Convert.ToByte(random.Next(0, 255)), Convert.ToByte(random.Next(0, 255)));
        storyboard.Begin(this);
    }

2 个答案:

答案 0 :(得分:2)

你能提供更多信息吗?另外,有没有理由说明为什么要在C#中声明这个故事板?通常,当我为多种颜色属性设置动画时,我会执行类似以下操作:

           <Storyboard x:Name="MyCoolStoryboard">

          <ColorAnimation 
            Storyboard.TargetName="ThingIwantToAnimate"
            Storyboard.TargetProperty="Color"
            From="Blue" To="Red" Duration="0:0:1" />
          <ColorAnimation 
            Storyboard.TargetName="ThingIwantToAnimate2"
            Storyboard.TargetProperty="Color"
            From="Blue" To="Red" Duration="0:0:1" /> 
          <ColorAnimation 
            Storyboard.TargetName="ThingIwantToAnimate3"
            Storyboard.TargetProperty="Color"
            From="Blue" To="Red" Duration="0:0:1" /> 
          <ColorAnimation 
            Storyboard.TargetName="ThingIwantToAnimate4"
            Storyboard.TargetProperty="Color"
            From="Blue" To="Red" Duration="0:0:1" /> 
        </Storyboard>

因为你在storyborad中包含了x:Name,你总是可以选择从C#代码启动它:

Storyboard sb = this.FindResource(“MyCoolStoryboard”)作为故事板;    sb.Begin();

答案 1 :(得分:1)

新答案:

我认为以前发布的动画可能会产生干扰,您可以试试这个:

  1. 每次重复使用相同的故事板,但停止之前启动的动画并清除其子项,
  2. 使用以下代码段启动动画:

    this.BeginStoryboard(stboard,HandoffBehavior.SnapshotAndReplace,true);

  3. 否则,每个动画都会有效吗?

    另外我注意到你订阅了每个完成的动画而不是只订阅故事板:为什么?


    旧答案:

    您好,

    你能提供整个代码吗? 另外:SpotLightWohnzimmerAnimation是一个类名还是一个实例?

    我通常按照此操作从代码http://blog.lexique-du-net.com/index.php?post/2009/07/07/CREATE%2C-LAUNCH-and-CONTROL-a-WPF-animation-FROM-CODE

    启动动画

    @Brandon:我认为他在XAML中宣传的东西就像聚光灯一样......创建动画可以通过在运行时自定义所有参数而不是设计时间来提高灵活性...... 1.