了解 WPF 窗口事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37974502/
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 13:51:23  来源:igfitidea点击:

Understand WPF Window Events

c#wpfevents

提问by Jonathan Wood

I see Windows have a Loadedevent, but not a Loadingevent (as there is Closingand Closedevents).

我看到的Windows有一个Loaded事件,而不是一个Loading事件(因为有ClosingClosed事件)。

My expectation was that the Loadedevent would occur before the window is actually displayed. However, looking at the Window Lifetime Events, it shows that the Loadedevent occurs after Activatedevent.

我的期望是该Loaded事件会在实际显示窗口之前发生。但是,查看Window Lifetime Events,它表明Loaded事件发生在Activated事件之后。

I put some initialization code in the Loadedevent and there is a delay after the window is displayed and before my content appears. So what is the best event handler to use for window initialization that should occur before the window is displayed?

我在Loaded事件中放入了一些初始化代码,在窗口显示之后和我的内容出现之前有一段延迟。那么,在窗口显示之前应该发生的窗口初始化的最佳事件处理程序是什么?

回答by Muneeb Zulfiqar

As documented in this codeprojectarticle:

正如此代码项目文章中所述:

Here is the complete life cycle of WPF

这是WPF的完整生命周期

Startup: (Application)

启动:(应用程序)

This is the first lifetime event of Application to get fired. This is fired when Application.Run() is called on the main window. But before the main window is shown. Please note that Application.Run() is abstracted from the user. If you want to check the compiler generated code, search under your projec\obj folder for file named App.g.i.cs (g stands for generated).

这是应用程序的第一个生命周期事件被触发。当在主窗口上调用 Application.Run() 时会触发它。但在显示主窗口之前。请注意 Application.Run() 是从用户中抽象出来的。如果要检查编译器生成的代码,请在您的 projec\obj 文件夹下搜索名为 App.gics 的文件(g 代表生成的)。

This is the event where you can process any commandline arguments received by your application.

这是您可以处理应用程序接收到的任何命令行参数的事件。

Initialized: (Window)

初始化:(窗口)

This is the first lifetime event of window to get fired. This is an ordinary .Net event and not a routed event. When this event is fired at window level it means all its nested child controls are also already initialized. So child controls will fire their Initialized event followed by parent control and finally by Window. This occurs once the window is instantiated. This event is at the level of base class FrameworkElement. Note that Styles and databinding will not be applied at this level. An interesting thing is FrameworkElement defines services for Animation, styles and DataBinding.

这是 window 的第一个生命周期事件被触发。这是一个普通的 .Net 事件,而不是路由事件。当在窗口级别触发此事件时,这意味着其所有嵌套的子控件也已初始化。所以子控件将触发它们的 Initialized 事件,然后是父控件,最后是 Window。一旦窗口被实例化,就会发生这种情况。此事件处于基类 FrameworkElement 的级别。请注意,此级别不会应用样式和数据绑定。一个有趣的事情是 FrameworkElement 为动画、样式和数据绑定定义了服务。

SourceInitialized : (Window)

源初始化:(窗口)

Acquire a proper legacy handle to your window. But still your window is not visible.

为您的窗口获取适当的旧式句柄。但是您的窗口仍然不可见。

Activated: (Application)

已激活:(应用程序)

Called every time when this app's window comes foreground and also the first time app's window is rendered. Please note that only after this event window's Activated event will be fired which actually makes sense

每次当这个应用程序的窗口出现在前台以及第一次呈现应用程序的窗口时调用。请注意,只有在此事件窗口的 Activated 事件之后才会被触发,这实际上是有意义的

Activated: (Window)

激活:(窗口)

Equivalent to GotFocus of a control. Called every time when window comes foreground and also the first time window is rendered. Only after this event window loaded event is fired.

相当于控件的 GotFocus。每次当窗口出现在前景以及第一次渲染窗口时调用。只有在触发此事件窗口加载事件之后。

Loaded: (Window)

加载:(窗口)

Your entire window is ready. Last stop before rendering. All animation, styles and databinding will be ready here. Any visual tweaks needs to be done here.

你的整个窗口都准备好了。渲染前的最后一站。所有动画、样式和数据绑定都将在这里准备就绪。任何视觉调整都需要在这里完成。

ContentRendered: (Window)

内容渲染:(窗口)

As the name implies it occurs after content was rendered. If there is no content this event will not be fired at all. Don't do any visual tweaks here. From here you may set a flag for your business logic that window is up, running and shown to the user for the first time.

顾名思义,它发生在呈现内容之后。如果没有内容,则根本不会触发此事件。不要在这里做任何视觉调整。从这里您可以为您的业务逻辑设置一个标志,即窗口第一次启动、运行并显示给用户。

Shutdown order of events:

事件关闭顺序:

Closing: (Window)

闭幕式:(窗口)

Trigger can be user action or programmatic. user action can be direct like clicking on close button.

触发器可以是用户操作或程序化。用户操作可以像单击关闭按钮一样直接。

Deactivated: (Window)

停用:(窗口)

Happens if window loses focus (this is window equivalent to control.GotFocus()) or closed.

如果窗口失去焦点(这相当于 control.GotFocus() 的窗口)或关闭时发生。

Deactivated: (Application)

停用:(应用程序)

Happens if user switches to another application (by Alt-Tab, taskbar or task manager etc.) or as effect of application closure.

如果用户切换到另一个应用程序(通过 Alt-Tab、任务栏或任务管理器等)或由于应用程序关闭而发生。

Closed: (Window)

关闭:(窗口)

Window is closed but window elements are accessible. Last place to access them if there is a reason. Note after this window unloaded will be fired.

窗口已关闭,但可以访问窗口元素。如果有原因,最后访问它们的地方。注意这个窗口卸载后会被触发。

Exit: (Application)

退出:(申请)

Last but not least. Only the Run() method returns. (please refer to startup event above for note on Run() method). But if you have written your custom main() method instead of a generated one still you can add some business logic code here. Any quick resource freeing can be done here.

最后但并非最不重要的。只有 Run() 方法返回。(Run() 方法的注意事项请参考上面的启动事件)。但是,如果您编写了自定义 main() 方法而不是生成的方法,您仍然可以在此处添加一些业务逻辑代码。任何快速的资源释放都可以在这里完成。