Brush 属性的 WPF ColorAnimation

发布于 2024-08-21 09:51:06 字数 1421 浏览 3 评论 0原文

我想知道是否有人可以帮助我 - 我有一个标签,当在后面的代码中调用方法时,我需要能够在任意两种颜色之间交叉淡入淡出。

到目前为止我最好的尝试:

Private OldColor as Color = Colors.White
Sub SetPulseColor(ByVal NewColor As Color)
    Dim F As New Animation.ColorAnimation(OldColor, NewColor, New Duration(TimeSpan.Parse("00:00:01")))
    OldColor = NewColor
    F.AutoReverse = False
    PulseLogo.BeginAnimation(Label.ForegroundProperty, F)

End Sub

我遇到的问题是 ColorAnimation 返回 Media.Color 并且 Foreground 的属性类型是 Brush。

我知道如何创建适当的画笔,但不知道如何在动画中执行此操作。

从谷歌搜索来看,我似乎需要一个转换器:

<ValueConversion(GetType(SolidColorBrush), GetType(SolidColorBrush))> _
Public Class ColorConverter
    Implements IValueConverter

Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Dim Color As Color = DirectCast(value, Color)
        Return New SolidColorBrush(Color)
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Return Nothing
    End Function

End Class

但是我见过的所有示例都将其绑定到 XAML 中的动画 - 而且我想在后面的代码中执行此操作...

有人可以指出我正确的方向吗?

谢谢

I wonder if someone can help me - I've got a label which I need to be able to cross-fade between any 2 colors when a method is called in the code behind.

My best attempt so far:

Private OldColor as Color = Colors.White
Sub SetPulseColor(ByVal NewColor As Color)
    Dim F As New Animation.ColorAnimation(OldColor, NewColor, New Duration(TimeSpan.Parse("00:00:01")))
    OldColor = NewColor
    F.AutoReverse = False
    PulseLogo.BeginAnimation(Label.ForegroundProperty, F)

End Sub

The problem I have is that ColorAnimation returns a Media.Color and The property type for Foreground is Brush.

I know how to create the appropriate brush but not how to do it in an animation.

From Googling, it seems I need a converter:

<ValueConversion(GetType(SolidColorBrush), GetType(SolidColorBrush))> _
Public Class ColorConverter
    Implements IValueConverter

Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Dim Color As Color = DirectCast(value, Color)
        Return New SolidColorBrush(Color)
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Return Nothing
    End Function

End Class

but all the examples I've seen bind it to the animation in XAML - And I'd like to do it in the code behind...

Can someone please point me in the right direction?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

萌辣 2024-08-28 09:51:06

通常的解决方案是不使用转换器,而是对画笔的颜色进行动画处理。但是,要执行此操作,您需要一个 PropertyPath,这又意味着您需要一个情节提要:(

Storyboard s = new Storyboard();
s.Duration = new Duration(new TimeSpan(0, 0, 1));
s.Children.Add(F);

Storyboard.SetTarget(F, PulseLogo);
Storyboard.SetTargetProperty(F, new PropertyPath("Foreground.Color"));

s.Begin();

请原谅 C# 语法)

请注意 SetTargetProperty 调用中的属性路径,该路径向下遍历 Foreground 属性并进入生成的画笔的 Color 属性。

您还可以使用此技术为渐变画笔等中的各个渐变停止点设置动画。

The usual solution to this is not to use a converter, but instead to animate the Color of the Brush. However, to do this you need a PropertyPath, which in turn means you need a storyboard:

Storyboard s = new Storyboard();
s.Duration = new Duration(new TimeSpan(0, 0, 1));
s.Children.Add(F);

Storyboard.SetTarget(F, PulseLogo);
Storyboard.SetTargetProperty(F, new PropertyPath("Foreground.Color"));

s.Begin();

(pardon C# syntax)

Note the property path in the SetTargetProperty call, which traverses down through the Foreground property and into the resulting brush's Color property.

You can also use this technique to animate individual gradient stops in a gradient brush, etc.

热风软妹 2024-08-28 09:51:06
            ColorAnimation colorChangeAnimation = new ColorAnimation();
            colorChangeAnimation.From = VariableColour;
             colorChangeAnimation.To = BaseColour;
            colorChangeAnimation.Duration = timeSpan;

            PropertyPath colorTargetPath = new PropertyPath("(Panel.Background).(SolidColorBrush.Color)");
            Storyboard CellBackgroundChangeStory = new Storyboard();
            Storyboard.SetTarget(colorChangeAnimation, BackGroundCellGrid);
            Storyboard.SetTargetProperty(colorChangeAnimation, colorTargetPath);
            CellBackgroundChangeStory.Children.Add(colorChangeAnimation);
            CellBackgroundChangeStory.Begin();

//变量颜色& BaseColour是Color类,timeSpan是TimeSpan类,BackGroundCellGrid是Grid类;

//无需在 XAML 中创建 SolidColorBrush 并绑定到它;
//玩得开心!

            ColorAnimation colorChangeAnimation = new ColorAnimation();
            colorChangeAnimation.From = VariableColour;
             colorChangeAnimation.To = BaseColour;
            colorChangeAnimation.Duration = timeSpan;

            PropertyPath colorTargetPath = new PropertyPath("(Panel.Background).(SolidColorBrush.Color)");
            Storyboard CellBackgroundChangeStory = new Storyboard();
            Storyboard.SetTarget(colorChangeAnimation, BackGroundCellGrid);
            Storyboard.SetTargetProperty(colorChangeAnimation, colorTargetPath);
            CellBackgroundChangeStory.Children.Add(colorChangeAnimation);
            CellBackgroundChangeStory.Begin();

//VariableColour & BaseColour are class of Color, timeSpan is Class of TimeSpan, BackGroundCellGrid is class of Grid;

//no need to create SolidColorBrush and binding to it in XAML;
//have fun!

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