在代码隐藏中对 LinearGradientBrush 颜色进行动画处理

发布于 2024-10-17 11:59:16 字数 741 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回首观望 2024-10-24 11:59:16

您的代码在 Silverlight 4 和 Windows Phone 7 下运行良好(WP7 几乎是 Silverlight 3)。我的猜测是,如果我为 SL3 构建一个独立的应用程序,它也可以在那里工作。

您的代码中唯一缺少的是如何首先获取 lgb ?您确定它与您的 UI 中实际使用的实例相同吗?

例如,我刚刚将画笔添加到我的 LayoutRoot 网格中,如下所示:-

<Grid x:Name="LayoutRoot">
    <Grid.Background>
        <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
            <GradientStop Color="Black" Offset="0" />
            <GradientStop Color="White" Offset="1" />
        </LinearGradientBrush>
    </Grid.Background>

然后在代码中我将 lgb 分配为:-

LinearGradientBrush lgb = (LinearGradientBrush)LayoutRoot.Background;

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:-

<Grid x:Name="LayoutRoot">
    <Grid.Background>
        <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
            <GradientStop Color="Black" Offset="0" />
            <GradientStop Color="White" Offset="1" />
        </LinearGradientBrush>
    </Grid.Background>

Then in code I assign lgb with:-

LinearGradientBrush lgb = (LinearGradientBrush)LayoutRoot.Background;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文