WPF StoryBoard.Completed 事件未触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14183121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:55:10  来源:igfitidea点击:

WPF StoryBoard.Completed event not firing

c#.netwpfstoryboard

提问by Marko Devcic

I have an animation before closing the main window, like the following code shows. Problem is the StoryBoard.Completedis not firing. Any clues what is causing this?

我在关闭主窗口之前有一个动画,如下面的代码所示。问题是StoryBoard.Completed没有开火。任何线索是什么导致了这种情况?

Code

代码

DoubleAnimation dblAnimX = new DoubleAnimation(1.0, 0.0, new  Duration(TimeSpan.FromSeconds(0.5)));
dblAnimX.SetValue(Storyboard.TargetProperty, this);

DoubleAnimation dblAnimY = new DoubleAnimation(1.0, 0.0, new Duration(TimeSpan.FromSeconds(0.5)));
dblAnimY.SetValue(Storyboard.TargetProperty, this);

Storyboard story = new Storyboard();
Storyboard.SetTarget(dblAnimX, this);
Storyboard.SetTarget(dblAnimY, this);
Storyboard.SetTargetProperty(dblAnimX, new PropertyPath("RenderTransform.ScaleX"));
Storyboard.SetTargetProperty(dblAnimY, new PropertyPath("RenderTransform.ScaleY"));

story.Children.Add(dblAnimX);
story.Children.Add(dblAnimY);
story.Begin(this);

story.Completed += (o, s) => { this.Close(); };

回答by Clemens

Add the Completed handler before calling Begin:

在调用 Begin 之前添加 Completed 处理程序:

story.Completed += (o, s) => Close();
story.Begin(this);


The reason for this behaviour is that the Completed handler is attached to an internal Clock object that is created during Begin. See the Remarks section in Completed:

此行为的原因是 Completed 处理程序附加到在 Begin 期间创建的内部时钟对象。请参阅已完成中的备注部分:

Although this event handler appears to be associated with a timeline, it actually registers with the Clock created for this timeline. For more information, see the Timing Events Overview.

尽管此事件处理程序看起来与时间线相关联,但它实际上注册了为此时间线创建的时钟。有关更多信息,请参阅时序事件概述。