Multiple animations in a Storyboard at the same time?
I created a few ColorAnimations and want them to run at the same time (whether they run syncronized doesn't matter). Sadly only one of them runs.
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.开发者_Go百科Next(0, 255)), Convert.ToByte(random.Next(0, 255)), Convert.ToByte(random.Next(0, 255)));
storyboard.Begin(this);
}
Could you provide some more info? Also, is there a reason as to why you are declaring this storyboard from within C#? Usually when I go abouts animating multiple color properties, I do something similar to the following:
<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>
And because you included the x:Name in the storyborad, you can always choose to start it from C# code by going:
Storyboard sb = this.FindResource("MyCoolStoryboard") as Storyboard; sb.Begin();
New answer :
I think that previously launched animation may be interfering and you may try this :
- Reuses the same storyboard each time but stop previously launched animation and clear its children,
Launch the animation with the below snippet :
this.BeginStoryboard(stboard, HandoffBehavior.SnapshotAndReplace, true);
Otherwise, does each animation work separetly ?
Also I notice that you subscribe to each animation completed instead of subscribing only to the storyboard : why ?
Old Answer :
Hello,
Can you provide the whole code ? Also : SpotLightWohnzimmerAnimation is a class'name or an instance ?
I usually follow this to launch animation from code : http://blog.lexique-du-net.com/index.php?post/2009/07/07/CREATE%2C-LAUNCH-and-CONTROL-a-WPF-animation-FROM-CODE
@Brandon: I think he is animating something not declared in its XAML like a spotlight... And creating animation allow you more flexibility by customizing all the parameters at runtime and not design time...1.
精彩评论