gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:01:57 27 4
gpt4 key购买 nike

我正在尝试在ColorAnimationFill上实现Ellipse,但是没有成功。 (我正在通过 View 模型中的Messenger模式调用的应用程序ColorAnimation背后的代码中实现此MainWindow。):

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);
});
}

...
}

我从 this StackOverflow question获得了 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);

关于wpf - 在椭圆填充中实现ColorAnimation的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57381886/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com