【问题标题】:Color animation not working彩色动画不起作用
【发布时间】:2011-05-09 19:17:43
【问题描述】:

我有一个名为“b”的按钮,我想将其背景从黑色更改为白色,但它不起作用。

错误:

“System.Windows.Media.Animation.ColorAnimation”动画对象不能用于动画属性“Background”,因为它的类型“System.Windows.Media.Brush”不兼容。

我的代码:

Dim changeColor As New Animation.ColorAnimation

changeColor.From = Colors.Black
changeColor.To = Colors.White
changeColor.Duration = TimeSpan.FromSeconds(0.2)

Animation.Storyboard.SetTarget(changeColor, b)
Animation.Storyboard.SetTargetProperty(changeColor, New PropertyPath(BackgroundProperty))

Dim sb As New Animation.Storyboard
sb.Children.Add(changeColor)
sb.Begin()

有什么想法吗?

【问题讨论】:

    标签: .net wpf vb.net silverlight animation


    【解决方案1】:

    值得注意的是,同样的问题可以在 XAML 中使用以下形式的表达式来解决:

    <ColorAnimation Duration="0:0:0.2" From="Black" To="White"
          Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
          Storyboard.TargetName="Body" />
    

    【讨论】:

    • 感谢这段代码 - 看来虽然我需要正确设置目标属性,但我还必须为我的边框添加默认背景颜色
    【解决方案2】:

    背景是 Brush 类型,不能使用 ColorAnimaion 进行动画处理。但是,SolidColorBrush 具有 Color 属性,因此您可以执行以下操作:

    Storyboard.SetTargetProperty(changeColor, new PropertyPath("Background.Color"));
    

    【讨论】:

    • 实际上它正在工作!我没有写正确的大小写。谢谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    相关资源
    最近更新 更多