1

我想将矩形的背景颜色更改为绿色 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));
4

1 回答 1

2

您可以使用ColorAnimationUsingKeyFrames

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);
于 2012-10-04T18:50:38.537 回答