首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.NET - ColorAnimation不工作

.NET - ColorAnimation不工作
EN

Stack Overflow用户
提问于 2010-09-22 15:54:31
回答 2查看 430关注 0票数 0

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

代码语言:javascript
复制
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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-22 16:16:19

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

代码语言:javascript
复制
Storyboard.SetTargetName(mouseEnterColorAnimation, mySpotlightAuditorium.Name);

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

代码语言:javascript
复制
Storyboard.SetTargetProperty(mouseEnterColorAnimation, new PropertyPath(SpotLightAuditorium.ColorProperty));

如果你想直接动画画笔(没有Name属性),你必须使用RegisterName在当前页面/用户控件/窗口中注册画笔的名称,这与使用XAML x:Name是一样的。

ALternativlely对于从Animatable派生的元素,可以使用以下方法

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2010-09-22 16:19:18

您没有在页面中注册画笔的名称,因此它可以作为情节提要的目标:

代码语言:javascript
复制
SolidColorBrush myAnimatedBrush = new SolidColorBrush();
myAnimatedBrush.Color = ?? choose a color

this.RegisterName("MyAnimatedBrush", myAnimatedBrush);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3767302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档