Wpf 如何创建没有淡入淡出效果的彩色动画

Wpf 如何创建没有淡入淡出效果的彩色动画,wpf,Wpf,我想将矩形的背景色改为绿色1秒,然后再改回黑色。我想模拟一盏灯是开着还是关着——我不想让颜色褪色。下面的代码符合我的要求,只是它会从黑色变为绿色,反之亦然。我绝对不想在UI线程上睡觉 ColorAnimation animation = new ColorAnimation { From = Colors.Black, To = Colors.LightGreen, Duration = new Duration(TimeSpan.FromSeconds(1)), RepeatBehavior=

我想将矩形的背景色改为绿色1秒,然后再改回黑色。我想模拟一盏灯是开着还是关着——我不想让颜色褪色。下面的代码符合我的要求,只是它会从黑色变为绿色,反之亦然。我绝对不想在UI线程上睡觉

ColorAnimation animation = new ColorAnimation { From = Colors.Black, To = Colors.LightGreen, Duration = new Duration(TimeSpan.FromSeconds(1)), RepeatBehavior= new RepeatBehavior(1), AutoReverse=true };
SolidColorBrush activityLight = new System.Windows.Media.SolidColorBrush(Colors.Black);
ActivityIndicator.Fill = activityLight;
this.RegisterName("activityLight", activityLight);
ActivityStoryboard = new Storyboard();
ActivityStoryboard.Children.Add(animation);
Storyboard.SetTargetName(animation, "activityLight");
Storyboard.SetTargetProperty(animation, new PropertyPath(SolidColorBrush.ColorProperty));
您可以使用:

var colorAnimation = new ColorAnimationUsingKeyFrames();
colorAnimation.KeyFrames.Add(
    new DiscreteColorKeyFrame(Colors.Green, TimeSpan.FromSeconds(0d)));
colorAnimation.KeyFrames.Add(
    new DiscreteColorKeyFrame(Colors.Black, TimeSpan.FromSeconds(1d)));

ActivityIndicator.Fill.BeginAnimation(SolidColorBrush.ColorProperty, colorAnimation);