我们的一位用户发送了我们 .NET Winforms 应用程序的日志,表明 Activated
事件正在发生 之前 Load
事件。我不认为这是可能的,并假设 Load
进行编码。总是会在 Activated
之前发生.
有没有其他人观察到Activated
发生在 Load
之前?
如果是这样,为什么以及有什么方法可以确保它不会发生?
最佳答案
来自 Order of Events in Windows Forms在 MSDN:
Application Startup and Shutdown Events
The Form and Control classes expose a set of events related to application startup and shutdown. When a Windows Forms application starts, the startup events of the main form are raised in the following order:
System.Windows.Forms.Control.HandleCreated
System.Windows.Forms.Control.BindingContextChanged
System.Windows.Forms.Form.Load
System.Windows.Forms.Control.VisibleChanged
System.Windows.Forms.Form.Activated
System.Windows.Forms.Form.Shown
When an application closes, the shutdown events of the main form are raised in the following order:
System.Windows.Forms.Form.Closing
System.Windows.Forms.Form.FormClosing
System.Windows.Forms.Form.Closed
System.Windows.Forms.Form.FormClosed
System.Windows.Forms.Form.Deactivate
您是否在任何启动事件中使用 MessageBox?由于 Windows 窗体消息循环处理对话框窗口的方式,这可能会导致事件显示为无序触发。
关于.net - Winforms - Load 和 Activated 事件的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1430955/