Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
736 views
in Technique[技术] by (71.8m points)

c# - WPF StoryBoard.Completed event not firing

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(); };
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add the Completed handler before calling Begin:

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...