我在下面尝试了代码,但是在行中有问题" storyboard.settargetProperty(CA1,new Propertypath(" gradientstop.colorproperty"((;
(;sb.begin(this(投掷了无效的exception,无法解决" gradstop1.colorproperty"。
如何正确地动画渐变式颜色?
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
LinearGradientBrush lgb = new LinearGradientBrush(
(Color)ColorConverter.ConvertFromString("#FF1E2838"),
(Color)ColorConverter.ConvertFromString("#FF2B364F"),
new Point(0.5, 0),
new Point(0.5, 1));
UCBody.Background = lgb;
ColorAnimation ca1 = new ColorAnimation(
(Color)ColorConverter.ConvertFromString("#FF1E2838"),
(Color)ColorConverter.ConvertFromString("#FF1E1E1E"),
TimeSpan.FromMilliseconds(600));
ColorAnimation ca2 = new ColorAnimation(
(Color)ColorConverter.ConvertFromString("#FF2B364F"),
(Color)ColorConverter.ConvertFromString("#FF2B2B38"),
TimeSpan.FromMilliseconds(600));
Storyboard sb = new Storyboard();
sb.Children.Add(ca1);
sb.Children.Add(ca2);
Storyboard.SetTargetName(ca1, "gradStop1");
Storyboard.SetTargetProperty(ca1, new PropertyPath("GradientStop.ColorProperty"));
Storyboard.SetTargetName(ca2, "gradStop2");
Storyboard.SetTargetProperty(ca2, new PropertyPath("GradientStop.ColorProperty"));
sb.Begin(this);
}
}
您没有名为" gradstop1"或" gradstop2"的对象,但是您可以使用BeginAnimation
方法来对LinearGradientBrush
的梯度停止进行动画:
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
LinearGradientBrush lgb = new LinearGradientBrush(
(Color)ColorConverter.ConvertFromString("#FF1E2838"),
(Color)ColorConverter.ConvertFromString("#FF2B364F"),
new Point(0.5, 0),
new Point(0.5, 1));
UCBody.Background = lgb;
ColorAnimation ca1 = new ColorAnimation(
(Color)ColorConverter.ConvertFromString("#FF1E2838"),
(Color)ColorConverter.ConvertFromString("#FF1E1E1E"),
TimeSpan.FromMilliseconds(600));
ColorAnimation ca2 = new ColorAnimation(
(Color)ColorConverter.ConvertFromString("#FF2B364F"),
(Color)ColorConverter.ConvertFromString("#FF2B2B38"),
TimeSpan.FromMilliseconds(600));
lgb.GradientStops[0].BeginAnimation(GradientStop.ColorProperty, ca1);
lgb.GradientStops[1].BeginAnimation(GradientStop.ColorProperty, ca2);
}