无法动画' Foreground.Color'在不可变对象实例上

时间:2016-10-18 08:54:56

标签: c# wpf

无法在不可变对象实例上设置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);
        }
    }

我不确定如何将动态动画添加到动态控件中。

1 个答案:

答案 0 :(得分:1)

为了实现动画效果,Run的Foreground Brush必须是可变的,默认值不是。

您应该在制作动画之前指定一个新的SolidColorBrush:

newLetter.Foreground = new SolidColorBrush(Colors.Black);