笔刷属性的WPF ColorAnimation

笔刷属性的WPF ColorAnimation,wpf,animation,converter,coloranimation,Wpf,Animation,Converter,Coloranimation,我想知道是否有人可以帮助我-我有一个标签,我需要能够交叉褪色之间的任何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"

我想知道是否有人可以帮助我-我有一个标签,我需要能够交叉褪色之间的任何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,前台的属性类型是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
_
公共类颜色转换器
实现IValueConverter
公共函数转换(ByVal值作为对象,ByVal targetType作为类型,ByVal参数作为对象,ByVal区域性作为System.Globalization.CultureInfo)作为对象实现System.Windows.Data.IValueConverter.Convert
暗显颜色为颜色=DirectCast(值,颜色)
返回新的SolidColorBrush(颜色)
端函数
公共函数ConvertBack(ByVal值作为对象,ByVal targetType作为类型,ByVal参数作为对象,ByVal区域性作为System.Globalization.CultureInfo)作为对象实现System.Windows.Data.IValueConverter.ConvertBack
一无所获
端函数
末级
但是我看到的所有示例都将它绑定到XAML中的动画-我想在代码隐藏中这样做

有人能给我指一下正确的方向吗


感谢

通常的解决方案不是使用转换器,而是为笔刷的颜色设置动画。但是,要做到这一点,您需要一个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调用中的属性路径,它向下遍历前台属性并进入结果笔刷的颜色属性

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

            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();
//可变颜色和基色是颜色的类别,timeSpan是timeSpan的类别,BackGroundCellGrid是网格的类别

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

那真的很优雅,我现在就试试。错误:无法对“System.Windows.Media.SolidColorBrush”上的“Color”属性设置动画,因为对象已密封或冻结。[我的代码]:Dim br As SolidColorBrush=DirectCast((PulseLogo.Foreground),SolidColorBrush)PulseLogo.Foreground.BeginAnimation(SolidColorBrush.ColorProperty,F)“感谢您的帮助抱歉,伙计,我搞砸了。我以前只在故事板上这样做过,我天真地认为我可以直接将其转化为一个BeginAnimation调用,这是错误的。我已经更新了答案,现在使用了实际的*gasp*诚实到善良的测试代码--希望这对您更有效。不用担心C#-它们如此相似,只不过是一种方言:)感谢更新的解决方案-它完美地工作了。+1;情节提要s=新情节提要();--缺少括号和时间跨度(大写S:)