0

我为 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);
4

2 回答 2

1

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

Storyboard.SetTargetName(mouseEnterColorAnimation, mySpotlightAuditorium.Name);

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

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

如果您想直接为 Brush 设置动画(没有 Name 属性),您必须使用 RegisterName 在当前 Page/UserControl/Window 中注册画笔的名称,这与使用 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);
于 2010-09-22T08:16:18.737 回答
0

您没有在页面上注册画笔的名称,以便它可以被故事板定位:

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

this.RegisterName("MyAnimatedBrush", myAnimatedBrush);
于 2010-09-22T08:19:17.920 回答