单击按钮时,我试图为按钮的背景颜色设置动画。
我遇到了一个问题,实际上抓住了定义按钮背景的画笔,所以我可以为它设置动画。这是一个linearGradientBrush。
我遇到的问题是我无法弄清楚实际为颜色设置动画的方法。当故事板开始时,它会抛出一个错误,告诉我“无法在不可变对象实例上设置动画'(0)”。
除此之外,我相信我没有正确地做到这一点。最终结果是将背景从其当前颜色设置为红色并返回,直到满足某些条件。
整体问题是......我的方法是否正确?是否有更好的方法来设置按钮背景颜色的动画效果?
private void RoundedButton_Click(object sender, RoutedEventArgs e)
{
//THIS OBVIOUSLY WON'T WORK BUT GIVES AN IDEA OF WHAT I'M TRYING TO ACCOMPLISH
LinearGradientBrush recBrush = ((SharedLibrary.Wpf.Controls.RoundedButton)sender).Background as LinearGradientBrush;
ColorAnimation pulserAnimation = new ColorAnimation(Colors.Red, new Duration(new TimeSpan(0, 0, 1)));
pulserAnimation.AutoReverse = true;
pulserAnimation.RepeatBehavior = RepeatBehavior.Forever;
Storyboard.SetTarget(pulserAnimation, recBrush);
Storyboard.SetTargetProperty(pulserAnimation, new PropertyPath(SolidColorBrush.ColorProperty));
Storyboard sb = new Storyboard();
//Add the animations to the new Storyboard
sb.Children.Add(pulserAnimation);
//run the animation
sb.Begin();
}