在代码隐藏中对 LinearGradientBrush 颜色进行动画处理
我正在尝试在 Silverlight 应用程序中为 LinearGradientBrush (lgb) 制作动画。我的页面的构造函数中有以下代码:
for (int stops = 0; stops < numStops; stops++)
{
ColorAnimation animation = new ColorAnimation();
animation.To =
Color.FromArgb(255, (byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256));
animation.Duration = TimeSpan.FromSeconds(1);
Storyboard.SetTarget(animation, lgb);
Storyboard.SetTargetProperty(animation,
new PropertyPath("GradientStops[" + stops.ToString() + "].Color"));
Storyboard story = new Storyboard();
story.Children.Add(animation);
story.Begin();
}
它编译并运行,但不会更改颜色。我只是不明白我做错了什么。
谢谢,
工作时间
I am playing a bit with trying to animate a LinearGradientBrush (lgb) in a Silverlight app. I have the following code in the constructor for my Page:
for (int stops = 0; stops < numStops; stops++)
{
ColorAnimation animation = new ColorAnimation();
animation.To =
Color.FromArgb(255, (byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256));
animation.Duration = TimeSpan.FromSeconds(1);
Storyboard.SetTarget(animation, lgb);
Storyboard.SetTargetProperty(animation,
new PropertyPath("GradientStops[" + stops.ToString() + "].Color"));
Storyboard story = new Storyboard();
story.Children.Add(animation);
story.Begin();
}
It compiles and runs, but doesn't change the color. I just don't see what I'm doing incorrectly.
Thanks,
wTs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码在 Silverlight 4 和 Windows Phone 7 下运行良好(WP7 几乎是 Silverlight 3)。我的猜测是,如果我为 SL3 构建一个独立的应用程序,它也可以在那里工作。
您的代码中唯一缺少的是如何首先获取 lgb ?您确定它与您的 UI 中实际使用的实例相同吗?
例如,我刚刚将画笔添加到我的 LayoutRoot 网格中,如下所示:-
然后在代码中我将 lgb 分配为:-
You code works fine under Silverlight 4 and in Windows Phone 7 (WP7 is pretty much Silverlight 3). My guess is if I build an isolated app for SL3 it would work there too.
The only thing missing from your code is how
lgb
is aquired in the first place? Are you sure its the same instance that is actually being used in your UI.For example, I just added the brush to my LayoutRoot Grid like this:-
Then in code I assign lgb with:-