本文介绍了WPF StoryBoard.Completed事件不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关闭主窗口,如下code表示之前的动画。问题是 StoryBoard.Completed 不点火。任何线索是什么原因造成的?

code

  DoubleAnimation是dblAnimX =新DoubleAnimation是(1.0,0.0,新的持续时间(TimeSpan.FromSeconds(0.5)));
dblAnimX.SetValue(Storyboard.TargetProperty,这一点);

DoubleAnimation是dblAnimY =新DoubleAnimation是(1.0,0.0,新持续时间(TimeSpan.FromSeconds(0.5)));
dblAnimY.SetValue(Storyboard.TargetProperty,这一点);

故事板故事=新的故事板();
Storyboard.SetTarget(dblAnimX,这一点);
Storyboard.SetTarget(dblAnimY,这一点);
Storyboard.SetTargetProperty(dblAnimX,新的PropertyPath(RenderTransform.ScaleX));
Storyboard.SetTargetProperty(dblAnimY,新的PropertyPath(RenderTransform.ScaleY));

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

story.Completed + =(O,S)=> {this.Close(); };
 

解决方案

在调用开始之前添加完成处理程序:

  story.Completed + =(O,S)=>关闭();
story.Begin(本);
 


我想这种现象的原因是,完成处理程序实际上是连接到一个时钟对象,这是在开始创建。请参阅Completed:

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(); };
解决方案

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:

这篇关于WPF StoryBoard.Completed事件不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:42