了解WPF窗口事件 [英] Understand WPF Window Events

查看:523
本文介绍了了解WPF窗口事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到Windows有一个 Loaded 事件,但没有一个 Loading 事件(因为有关闭 Closed 事件)。

I see Windows have a Loaded event, but not a Loading event (as there is Closing and Closed events).

我的期望是<$在实际显示窗口之前,将发生c $ c> Loaded 事件。但是,请查看窗口生命周期事件 ,则表明 Loaded 事件在 Activated 事件之后发生。

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

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

I put some initialization code in the Loaded event 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?

推荐答案

codeproject 文章:

这是WPF的完整生命周期

Here is the complete life cycle of WPF

启动:(应用程序)

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

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).

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

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

已初始化:(窗口)

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

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.

SourceInitialized:(Window)

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

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

已激活:(应用程序)

每次当此应用程序的窗口进入前景并且首次渲染该应用程序的窗口时调用。请注意,只有在此事件窗口的Activated事件被触发后,这才有意义

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

已激活:(窗口)

等效于控件的GotFocus。每次在窗口进入前景并且第一次渲染窗口时调用。仅在触发此事件后,才触发窗口加载事件。

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.

已加载:(窗口)

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

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 :(窗口)

顾名思义,它出现在内容之后被渲染。如果没有内容,则完全不会触发此事件。在这里不要做任何视觉上的调整。在这里,您可以为业务逻辑设置一个标志,表示该窗口是第一次打开,正在运行并首次向用户显示。

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:

关闭:(窗口)

触发器可以是用户操作或编程的。用户操作可以是直接的,例如单击关闭按钮。

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

已停用:(窗口)

如果窗口失去焦点,则会发生(这是

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

已停用:(应用程序)

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

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

已关闭:(窗口)

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

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.

退出:(应用程序)

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

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.

这篇关于了解WPF窗口事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆