WPF 控件开发之 Lefitime Events for all Elements

刚好做东东遇到这个问题,回头把MSDN和<Pro WPF in C# 2008>又翻了翻,记录下来当作备份。

如果以后还有人问,直接给ge链接给他,嘿嘿!

 

 

在WPF中,Element大体上来说有3个阶段. 并且有3个对应的事件

1. Initialized

2. Loaded

3. Unloaded

具体的内容大家可以看看MSDN的说明:

http://msdn.microsoft.com/zh-cn/library/ms754221.aspx

 

看完了后,这儿按顺序说说相关的一些东西


Step1:BeginInit()

    Xaml 解析器开始干活,Xaml被改变的的DP开始赋值

 

Step2: EndInit()

1. 完成初始化工作,发Initialized事件

2. 因为整棵LogicTree是按“叶子-〉根”的顺序创建的。所以现在当前Element它的logicTree已经都创建完成。但是请注意:

    1)  你不能说包含该Element的更大的LogicTree也被成功创建了!

    2)  该Element的Style设置,数据绑定并不一定完成!

 

Step3:Loaded

当所有的Element都Initialized后,Loaded 事件被触发

1.  按“根-〉叶子”的顺序发Loaded event,刚好和Initialized相反。

2. 当所有Element的Loaded事件都发完后,Window开始显示出来,并且开始Render Element

 

Step4:UnLoaded

好像这儿不用多说什么了

 

在Element的生存周期里,除了上面的,还有其他的一些状态,不想翻译组织了,直接抄过来,就是上面提到的那本书。大家也可以到MSDN中去看看

Name

Description

SourceInitialized

Occurs when the HwndSource property of the window is acquired (but before the window is made visible). The HwndSource is a window handle that you may need to use if you’re calling legacy functions in the Win32 API.

ContentRendered

Occurs immediately after the window has been rendered for the first time. This isn’t a good place to perform any changes that might affect the visual appearance of the window, or you’ll force a second render operation. (Use the Loaded event instead.) However, the ContentRendered event does indicate that your window is fully visible and ready for input.

Activated

Occurs when the user switches to this window (for example, from another window in your application or from another application). Activated also fires when the window is loaded for the first time. Conceptually, the Activated event is the window equivalent of a control’s GotFocus event.

Deactivated

Occurs when the user switches away from this window (for example, by moving to another window in your application or another application). Deactivated also fires when the window is closed by a user, after the Closing event but before Closed. Conceptually, the Deactivated event is the window equivalent of a control’s LostFocus event.

Closing

Occurs when the window is closed, either by a user action or programmatically using the Window.Close() method or the Application.Shutdown() method. The Closing event gives you the opportunity to cancel the operation and keep the window open by setting the CancelEventArgs.Cancel property to true. However, you won’t receive the Closing event if your application is ending because the user is shutting down the computer or logging off. To deal with these possibilities, you need to handle the Application.SessionEnding

event described in Chapter 3.

Closed

Occurs after the window has been closed. However, the element objects are still accessible, and the Unloaded event hasn’t fired yet. At this point, you can perform cleanup, write settings to a persistent storage place (such as a configuration file or the Windows registry), and so on.

 

 

 另外,如果你只想做一些简单的初始化设置,建议在Loaded 时去完,因为现在的Style,数据绑定等等都搞定了,可以说Element已经真正地完成了初始化创建。

 


你可能感兴趣的:(element)