我创建了一些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);
}
答案 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)
新答案:
我认为以前发布的动画可能会产生干扰,您可以试试这个:
使用以下代码段启动动画:
this.BeginStoryboard(stboard,HandoffBehavior.SnapshotAndReplace,true);
否则,每个动画都会有效吗?
另外我注意到你订阅了每个完成的动画而不是只订阅故事板:为什么?
旧答案:
您好,
你能提供整个代码吗? 另外:SpotLightWohnzimmerAnimation是一个类名还是一个实例?
启动动画@Brandon:我认为他在XAML中宣传的东西就像聚光灯一样......创建动画可以通过在运行时自定义所有参数而不是设计时间来提高灵活性...... 1.