在椭圆填充中实现 ColorAnimation 的问题

Problems implementing ColorAnimation in Ellipse Fill

我正在尝试在 EllipseFill 上实施 ColorAnimation,但没有成功。 (我在我的应用程序 MainWindow 的代码隐藏中实现此 ColorAnimation,通过视图模型中的 Messenger 模式调用。):

public partial class MainWindow : Window
{
    private SolidColorBrush fillBrush = new SolidColorBrush(Colors.Red);
    private ColorAnimation ca = new ColorAnimation(Colors.Red, Colors.Yellow, new Duration(TimeSpan.FromMilliseconds(800)));
    private Storyboard flashEllipse = new Storyboard();


    public MainWindow(MainWindowVM viewModel)
    {
        DataContext = viewModel;
        InitializeComponent();

        Messenger.Default.Register<string>(this, StartFlashEllipse, Constants.StartMsg);
        Messenger.Default.Register<string>(this, StopFlashEllipse, Constants.StopMsg);

        RegisterName("EllipseFillBrush", fillBrush);
        Storyboard.SetTargetName(ca, "EllipseFillBrush");
        Storyboard.SetTargetProperty(ca, new PropertyPath("(0).(1)", Ellipse.FillProperty, SolidColorBrush.ColorProperty));
        flashEllipse.Children.Add(ca);
        flashEllipse.RepeatBehavior = RepeatBehavior.Forever;
    }


    private void StartFlashEllipse(string ellipseTag)
    {
        Dispatcher.Invoke(DispatcherPriority.Render, (Action)delegate ()
        {
            Ellipse ellipseToFlash = <code to find Ellipse in Window>;

            ellipseToFlash.Fill = new SolidColorBrush(Colors.Blue);
            flashEllipse.Begin(ellipseToFlash);
        });
    }

    ...
}

我从 获得了 PropertyPath 代码,但我仍然在 flashEllipse.Begin() 行上收到与此问题的 OP 类似的异常:

''Fill' property does not point to a DependencyObject in path '(0).(1)'.'

我在这里错过了什么?

您不需要 Storyboard 和复杂的 PropertyPath。

直接启动SolidColorBrush的Color属性动画即可:

ellipseToFlash.Fill = new SolidColorBrush(Colors.Blue);

ellipseToFlash.Fill.BeginAnimation(SolidColorBrush.ColorProperty, ca);