WPF StoryBoard.Completed event not firing

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

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

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

回答1:

Add the Completed handler before calling Begin:

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

I guess the reason for this behaviour is that the Completed handler is actually attached to a Clock object, which is created during Begin. See the Remarks section in Completed:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!