[英]Cannot animate 'Foreground.Color' on an immutable object instance
Cannot animate 'Foreground.Color on an immutable object instance. 无法在不可变对象实例上为'Foreground.Color设置动画。
private void AnimColor()
{
int counter = 0;
Storyboard mystoryboard = new Storyboard();
foreach (char letter in txtSend.Text)
{
Run newLetter = new Run(letter.ToString());
newLetter.Name = "letter_" + counter.ToString();
textblock1.Inlines.Add(newLetter);
counter++;
ColorAnimation k = new ColorAnimation();
//MessageBox.Show(letter.Name);
Storyboard.SetTarget(k, newLetter);
Storyboard.SetTargetProperty(k, new PropertyPath("Foreground.Color"));
k.From = Colors.Red;
k.To = Colors.Blue;
k.Duration = TimeSpan.FromSeconds(0.5);
k.BeginTime = TimeSpan.FromSeconds(0.5 * counter);
mystoryboard.Children.Add(k);
}
try
{
mystoryboard.Begin(this);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I'm not exactly sure how to add a dynamic animation to a dynamic control. 我不确定如何将动态动画添加到动态控件中。
In order to be animatable, the Foreground Brush of the Run must be mutable, which the default value isn't. 为了具有动画效果,“运行”的“前景画笔”必须是可变的,默认值不是。
You should assign a new SolidColorBrush before animating: 设置动画之前,您应该分配一个新的SolidColorBrush:
newLetter.Foreground = new SolidColorBrush(Colors.Black);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.