我为一个聚光灯对象创建了一个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);发布于 2010-09-22 16:16:19
使用Storyboard.SetTargetName时,名称必须是要在其中设置属性动画的FrameworkElement实例的实际Name属性的值。因此,在您的示例中,可能是SpotLightAuditorium控件的实例:
Storyboard.SetTargetName(mouseEnterColorAnimation, mySpotlightAuditorium.Name);属性路径必须是对实际依赖属性的引用:
Storyboard.SetTargetProperty(mouseEnterColorAnimation, new PropertyPath(SpotLightAuditorium.ColorProperty));如果你想直接动画画笔(没有Name属性),你必须使用RegisterName在当前页面/用户控件/窗口中注册画笔的名称,这与使用XAML x:Name是一样的。
ALternativlely对于从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-22 16:19:18
您没有在页面中注册画笔的名称,因此它可以作为情节提要的目标:
SolidColorBrush myAnimatedBrush = new SolidColorBrush();
myAnimatedBrush.Color = ?? choose a color
this.RegisterName("MyAnimatedBrush", myAnimatedBrush);https://stackoverflow.com/questions/3767302
复制相似问题