gpt4 book ai didi

画笔属性的 WPF ColorAnimation

转载 作者:行者123 更新时间:2023-12-03 21:35:15 26 4
gpt4 key购买 nike

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

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

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

但是我看到的所有示例都将它绑定(bind)到 XAML 中的动画 - 我想在后面的代码中执行它......

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

谢谢

最佳答案

通常的解决方案是不使用转换器,而是为画笔的颜色设置动画。但是,要做到这一点,您需要一个 PropertyPath,这反过来意味着您需要一个 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();

(请原谅 C# 语法)

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

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

关于画笔属性的 WPF ColorAnimation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2248726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com