Bor*_*ris 7 wpf animation coloranimation
我有一个网格,一个窗口根元素.我想应用一个动画,它会在5秒内将背景颜色从白色变为绿色.这是我做的:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    ColorAnimation animation;
    animation = new ColorAnimation();
    animation.From = Colors.White;
    animation.To = Colors.Green;
    animation.Duration = new Duration(TimeSpan.FromSeconds(5));
    rootElement.BeginAnimation(Grid.BackgroundProperty, animation);
}
代码不起作用.什么都没有改变.我哪里弄错了?谢谢.
Bor*_*ris 15
解决了!
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    SolidColorBrush rootElementBrush;
    ColorAnimation animation;
    rootElementBrush = this.FindResource("RootElementBrush") as SolidColorBrush;
    // Animate the brush 
    animation = new ColorAnimation();
    animation.To = Colors.Green;
    animation.Duration = new Duration(TimeSpan.FromSeconds(5));
    rootElementBrush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
}
这是一个解释:
我最初的错误是我想Grid.BackgroundProperty通过为它分配颜色来改变它,但它接受了刷子而不是苹果和橘子!因此,我创建了一个SolidColorBrush静态资源并将其命名为rootElementBrush.在XAML中,我将Grid rootElementbackground属性设置为该静态资源.最后,我修改了动画,所以现在它改变了颜色SolidColorBrush.简单!
THE*_*TOR 12
尝试一下:
<ColorAnimation
Storyboard.TargetName="PlayButtonArrow" 
Storyboard.TargetProperty="Fill.Color"
From="White"
To="Green"              
Duration="0:0:5.0"
AutoReverse="False"/>
| 归档时间: | 
 | 
| 查看次数: | 24150 次 | 
| 最近记录: |