简体   繁体   English

无法在不可变对象实例上设置“ Foreground.Color”动画

[英]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.

相关问题 : '无法在不可变对象实例上为 'Fill.Color' 设置动画。 - : 'Cannot animate 'Fill.Color' on an immutable object instance.' 错误消息“无法在不可变对象实例上为'(0)。(1)'设置动画”的按钮,带有RelayCommand - Error message “Cannot animate '(0).(1)' on an immutable object instance” for Button with RelayCommand 使用带故事板抛出的DataTrigger的WPF图像无法在不可变对象实例上设置'((RenderTransform)。(RotateTransform.Angle)'动画 - WPF Image using DataTrigger with Storyboard throwing Cannot animate '((RenderTransform).(RotateTransform.Angle)' on an immutable object instance 列表框选择产生无法动画化不可变对象错误 - Listbox selection produces Cannot Animate Immutable Object error 如果前景绑定到属性,我可以以某种方式为文本块的前景颜色设置动画吗? - Can I somehow animate foreground color of textblock if foreground is binded to a property? 由于对象已密封或冻结,因此无法设置颜色属性的动画 - Cannot animate the color property because the object is sealed or frozen 如何为标签的前景颜色变化添加动画或添加过渡? - How to animate or add transition to a label's foreground color change? 不变变量和不变对象 - immutable variable and immutable object VS dll 引用错误 - 实例对象被创建为不可变对象 - VS dll reference error - Instance object was created as immutable DataContractSerializer和不可变类型。 反序列化为已知对象实例 - DataContractSerializer and immutable types. Deserialising to a known object instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM