15

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

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

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 中的动画 - 我想在后面的代码中执行它......

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

谢谢

4

2 回答 2

22

通常的解决方案是不使用转换器,而是为画笔的颜色设置动画。但是,要做到这一点,您需要一个 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 属性。

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

于 2010-02-12T00:38:00.883 回答
0
            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是Color的类,timeSpan是TimeSpan的类,BackGroundCellGrid是Grid的类;

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

于 2014-03-08T20:43:08.720 回答