.NET - ColorAnimation不起作用

时间:2010-09-22 07:54:31

标签: c# .net visual-studio coloranimation

我为SpotLight对象创建了一个ColorAnimation,但它似乎不起作用。 我做错了什么?

ColorAnimation mouseEnterColorAnimation = new ColorAnimation();
mouseEnterColorAnimation.To = Colors.Red;
mouseEnterColorAnimation.Duration = TimeSpan.FromSeconds(5);
Storyboard.SetTargetName(mouseEnterColorAnimation, "MyAnimatedBrush");

Storyboard.SetTargetProperty(mouseEnterColorAnimation, new PropertyPath(SpotLightAuditorium.Color));
Storyboard storyboard = new Storyboard();
storyboard.RepeatBehavior = RepeatBehavior.Forever;
storyboard.Children.Add(mouseEnterColorAnimation);
storyboard.Begin(this);

2 个答案:

答案 0 :(得分:1)

使用Storyboard.SetTargetName时,名称必须是要为属性设置动画的FrameworkElement实例的实际Name属性的值。所以在你的情况下可能是SpotLightAuditorium控件的一个实例:

Storyboard.SetTargetName(mouseEnterColorAnimation, mySpotlightAuditorium.Name);

propertypath必须是对实际依赖项属性的引用:

Storyboard.SetTargetProperty(mouseEnterColorAnimation, new PropertyPath(SpotLightAuditorium.ColorProperty));

如果要直接为画笔设置动画(没有Name属性),则必须使用RegisterName在当前页面/用户控件/窗口中注册画笔名称。这与使用XAML相同x:Name

另外,您可以对从Animatable派生的元素使用以下方法:

ColorAnimation mouseEnterColorAnimation = new ColorAnimation();
mouseEnterColorAnimation.To = Colors.Red;
mouseEnterColorAnimation.Duration = TimeSpan.FromSeconds(5);
myAnimatedBrush.BeginAnimation(SolidColorBrush.ColorProperty, null); // remove the old animation to prevent memoryleak
myAnimatedBrush.BeginAnimation(SolidColorBrush.ColorProperty, mouseEnterColorAnimation);

答案 1 :(得分:0)

您没有在页面中注册画笔名称,因此可以通过故事板进行定位:

SolidColorBrush myAnimatedBrush = new SolidColorBrush();
myAnimatedBrush.Color = ?? choose a color

this.RegisterName("MyAnimatedBrush", myAnimatedBrush);