'I need an example of animating a foreground brush on a text block in either UWP or WinUI

I want to animate the color of text when something changes. I can't find any code examples (other than ones that use the Storyboard, which I want to avoid). I found this:

  var animation = _compositor.CreateColorKeyFrameAnimation(); 
  animation.InsertKeyFrame(0.0f, Color.FromArgb(255, 0, 255, 0));
  animation.InsertKeyFrame(1.0f, Colors.Purple);
  targetVisual.Brush.StartAnimation("Color", animation);

But can't figure out how to connect this concept to a plain, old foreground brush on a TextBlock.



Solution 1:[1]

You could try the following code:

  Storyboard _storyboard = new Storyboard();

        ColorAnimation colorAni = new ColorAnimation();
        colorAni.To = Colors.Red;
        colorAni.Duration = new Duration(TimeSpan.FromSeconds(2)); 

        Storyboard.SetTarget(colorAni, MyBlock);
        Storyboard.SetTargetProperty(colorAni, "(TextBlock.Foreground).(SolidColorBrush.Color)");

        _storyboard.Children.Add(colorAni);

        _storyboard.Begin();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Roy Li - MSFT