简体   繁体   English

C#WPF:GradientStop.ColorProperty动画在情节提要中不起作用

[英]C# WPF: GradientStop.ColorProperty Animation Doesn't Work In Storyboard

I've recently been developing a WPF in C# and came across the above problem. 我最近一直在用C#开发WPF,遇到了上述问题。 My original code, which worked, is: 我有效的原始代码是:

ColorAnimation backgroundfade = ClrAnim(CanvasGS2.Color, Color.FromRgb(5, 3, 13), 1, 0.8, 0.1);
backgroundfade.BeginTime = TimeSpan.FromSeconds(1.3);
CanvasGS2.BeginAnimation(GradientStop.ColorProperty, backgroundfade);

However, when I add it to a storyboard, everything runs fine, but the animation doesn't happen. 但是,当我将其添加到情节提要中时,一切运行良好,但动画没有发生。 To provide context, the first animation in the following code happens, but the second one doesn't: 为了提供上下文,以下代码中的第一个动画发生了,但是第二个动画没有发生:

DoubleAnimation labeltotopleft = DblAnim((double)((Label)selectedlabel).GetValue(Canvas.LeftProperty), 50, 1, 0.8, 0.2);
labeltotopleft.BeginTime = TimeSpan.FromSeconds(0.7);
InitialiseInnerMenu.Children.Add(labeltotopleft);
Storyboard.SetTarget(labeltotopleft, (Label)selectedlabel);
Storyboard.SetTargetProperty(labeltotopleft, new PropertyPath(Canvas.LeftProperty));
//((Label)selectedlabel).BeginAnimation(LeftProperty, labeltotopleft);

ColorAnimation backgroundfade = ClrAnim(CanvasGS2.Color, Color.FromRgb(5, 3, 13), 1, 0.8, 0.1);
backgroundfade.BeginTime = TimeSpan.FromSeconds(1.3);
InitialiseInnerMenu.Children.Add(backgroundfade);
Storyboard.SetTarget(backgroundfade, CanvasGS2);
Storyboard.SetTargetProperty(backgroundfade, new PropertyPath(GradientStop.ColorProperty));
//CanvasGS2.BeginAnimation(GradientStop.ColorProperty, backgroundfade);

The original code is commented out - both animations worked when I did it that way (along with the top two lines of each block). 原始代码已被注释掉-当我这样做时,两个动画都起作用(以及每个块的前两行)。

Is it a problem with the property path? 属性路径是否有问题?

Thanks 谢谢

Chris 克里斯

I finally have an answer to this question, which I discovered while researching rotation animations. 我终于有了这个问题的答案,这是我在研究旋转动画时发现的。

One method that works for these more abstract properties is making them “namescoped”, and then using Storyboard.SetTargetName rather than storyboard Storyboard.SetTarget. 适用于这些更抽象的属性的一种方法是使它们成为“名称范围”,然后使用Storyboard.SetTargetName而不是Storyboard Storyboard.SetTarget。 The above code can be remedied as follows: 上面的代码可以按如下方式纠正:

Didn't work: 没用:

ColorAnimation backgroundfade = ClrAnim(CanvasGS2.Color, Color.FromRgb(5, 3, 13), 1, 0.8, 0.1);
backgroundfade.BeginTime = TimeSpan.FromSeconds(1.3);
InitialiseInnerMenu.Children.Add(backgroundfade);
Storyboard.SetTarget(backgroundfade, CanvasGS2);
Storyboard.SetTargetProperty(backgroundfade, new PropertyPath(GradientStop.ColorProperty));

Does work: 可以工作:

ColorAnimation backgroundfade = ClrAnim(CanvasGS2.Color, Color.FromRgb(5, 3, 13), 1, 0.8, 0.1);
backgroundfade.BeginTime = TimeSpan.FromSeconds(1.3);
InitialiseInnerMenu.Children.Add(backgroundfade);
try { UnregisterName("CanvasGS2"); }
catch { }
finally { RegisterName("CanvasGS2", CanvasGS2); }
Storyboard.SetTargetName(backgroundfade, "CanvasGS2");
Storyboard.SetTargetProperty(backgroundfade, new PropertyPath(GradientStop.ColorProperty));

Another thing that needs mentioning is that, when it comes to beginning the storyboard, the following will not work: 需要提及的另一件事是,在开始情节提要时,以下内容将不起作用:

InitialiseInnerMenu.Begin();

What's needed is this: 需要的是:

InitialiseInnerMenu.Begin(this);

The object name has been “namescoped” to “this”, which is simply the MainWindow (and the default namescope). 对象名称已由“名称范围”更改为“此”,这就是MainWindow(和默认名称范围)。


Another way to achieve this is to target the UIElement itself, in this case a Canvas, and use an appropriate property path (for me the only drawback is that these are somewhat challenging to put together, although probably learnable in the long run), given as a string. 实现此目标的另一种方法是将UIElement本身(在这种情况下为Canvas)作为目标,并使用适当的属性路径(对我而言,唯一的缺点是将这些元素放在一起会有些挑战,尽管从长远来看可能是可以学习的)作为字符串。 This saves all the registering of names and having to pass a parameter to Storyboard.Begin(). 这样可以节省所有名称的注册,并且不必将参数传递给Storyboard.Begin()。

First the XAML for clarity: 首先,为了清楚起见,使用XAML:

<Canvas x:Name="MainCanvas" >
    <Canvas.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="Black"/>
            <GradientStop x:Name="CanvasGS2" Color="#FF0E0727" Offset="1"/>
        </LinearGradientBrush>
    </Canvas.Background>
</Canvas>

And the code-behind: 和背后的代码:

ColorAnimation backgroundfade = ClrAnim(CanvasGS2.Color, Color.FromRgb(5, 3, 13), 1, 0.8, 0.1);
backgroundfade.BeginTime = TimeSpan.FromSeconds(1.3);
InitialiseInnerMenu.Children.Add(backgroundfade);
Storyboard.SetTarget(backgroundfade, MainCanvas);
Storyboard.SetTargetProperty(backgroundfade, new PropertyPath("Background.(GradientBrush.GradientStops)[1].(GradientStop.Color)"));
CanvasGS2.BeginAnimation(GradientStop.ColorProperty, backgroundfade);

Information sources: 信息来源:

Storyboard doesn't work 情节提要不起作用

Animate color property with different brushes 使用不同的笔刷对颜色属性进行动画处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM