c# - 如何在较长的 Storyboard 中的 DoubleAnimation 末尾调用回调?

标签 c# wpf animation

我有一个包含多个 DoubleAnimations 的 Storyboard。我想在其中一个 child 完成时执行一个功能。但是,如果我将 EventHandler 附加到 DoubleAnimations 之一的 Completed 事件,则在整个 Storyboard 完成之前不会调用它。

private void Window_Activated(object sender, EventArgs e)
{
    Storyboard myStoryboard = new Storyboard();

    DoubleAnimation anim1 = new DoubleAnimation(0, 500, new Duration(new TimeSpan(0, 0, 2)));
    anim1.Completed += anim1_Complete;
    Storyboard.SetTarget(anim1, circle);
    Storyboard.SetTargetProperty(anim1, new PropertyPath("(Canvas.Left)"));
    myStoryboard.Children.Add(anim1);

    DoubleAnimation anim2 = new DoubleAnimation(500, new Duration(new TimeSpan(0,0,2)));
    anim2.BeginTime = new TimeSpan(0,0,2);
    Storyboard.SetTarget(anim2, circle);
    Storyboard.SetTargetProperty(anim2, new PropertyPath("(Canvas.Top)"));
    myStoryboard.Children.Add(anim2);
    myStoryboard.Begin();
}

void anim1_Complete(object sender, EventArgs e)
{
    Console.WriteLine("anim 1 complete");
}

当其中一个子动画完成时,我如何得到通知?

我意识到,在这个极简主义示例中,我提供了创建两个 Storyboard(或动画)并让第一个 Storyboard的 Complete 事件触发第二个 Storyboard的开始的简单方法。但是,这不是我正在寻找的答案。 我希望能够维护一个主时间线,我可以暂停、继续、寻找、倒退等,并在子时间线的开始或结束经过时得到通知。

最佳答案

假设那个圆是一个 UIElement 试试这个:

DoubleAnimation anim1 = new DoubleAnimation(0, 500, new Duration(new TimeSpan(0, 0, 2)));
anim1.Completed += anim1_Complete;

circle.BeginAnimation(Canvas.LeftProperty, anim1);

其他动画也一样。 所以没有 Storyboard。

关于c# - 如何在较长的 Storyboard 中的 DoubleAnimation 末尾调用回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25878429/

相关文章:

wpf - 将多个参数传递给 Prism 的 EventAggregator

javascript - 单击时无法调整循环 "cloud"图像大小

c# webforms 测试策略

c# - System.Windows.Interactivity : Could not load file or assembly 'System. Windows,版本=2.0.5.0

C#、WPF、MVVM、为消息对话框/确认对话框创建 View 模型?

c# - WPF:特定输入文本框(用于电话号码等)

javascript - 检查页面何时加载以及打开动画何时完成

javascript - 将元素动画到其在 DOM 中的新位置

c# - 有没有办法用 DescriptorProto 反序列化未知对象?

c# - 无法评估表达式,因为代码已优化或 native 框架位于调用堆栈顶部