如何为 GradientStop 颜色设置动画?
How to animate GradientStop color?
我试过下面的代码,但是 "Storyboard.SetTargetProperty(ca1, new PropertyPath("GradientStop.ColorProperty"));
行有问题
sb.Begin(this) 抛出 InvalidOperationException 并且无法解析“gradStop1.ColorProperty”。
如何正确设置 GradientStop 颜色的动画?
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);
}
我试过下面的代码,但是 "Storyboard.SetTargetProperty(ca1, new PropertyPath("GradientStop.ColorProperty"));
行有问题sb.Begin(this) 抛出 InvalidOperationException 并且无法解析“gradStop1.ColorProperty”。
如何正确设置 GradientStop 颜色的动画?
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);
}