使用WPF在运行时标记背景动画

时间:2010-02-26 13:59:16

标签: wpf vb.net animation

我想以编程方式创建标签背景颜色的动画,但我遇到了一些问题。

我已经实现了以下代码:

  Public Sub DoBackgroundAnimation(ByVal obj As Label)

        If obj Is Nothing Then Return

        Dim animatedBrush As New SolidColorBrush()
        animatedBrush.Color = Colors.MidnightBlue

        Dim highlightAnimation As New ColorAnimation()
        highlightAnimation.[To] = Colors.Transparent
        highlightAnimation.Duration = TimeSpan.FromSeconds(1)
        Storyboard.SetTarget(highlightAnimation, animatedBrush)
        Storyboard.SetTargetProperty(highlightAnimation, New PropertyPath(SolidColorBrush.ColorProperty))

        Dim story As New Storyboard
        story.Children.Add(highlightAnimation)

        obj.Background = animatedBrush
        story.Begin(obj)

    End Sub

但没有任何反应

背景颜色简单是MidnightBlue,没有动画。

你有什么建议吗?

2 个答案:

答案 0 :(得分:2)

我昨晚遇到的问题(请参阅here)是,只有在您制作动画的属性属于Storyboard.SetTarget或{{1}的属性时才使用FrameworkElement }。

您实际上并未动画FrameworkContentElement,而是为Label.Background制作动画。所以(至少,据我所知)你必须创建一个名称范围,给你的画笔命名,并使用SolidColorBrush.Color将其设置为目标。

此方法适用于C#;将它翻译成VB应该很简单:

Storyboard.SetTargetName

答案 1 :(得分:0)

我今天早些时候看到了the same problem。但它是关于渲染transfrom和C#。

简短的回答是将标签作为动画目标传递,并将属性路径更改为(Label.Background).(SolidColorBrush.Color)The long answer涉及使用名称范围。

希望这有帮助。

干杯,安瓦卡。