物件存留期事件Object Lifetime Events

本主題說明特定的 WPFWPF 事件,表示物件的建立、使用和解構存留期階段。This topic describes the specific WPFWPF events that signify stages in an object lifetime of creation, use, and destruction.


本主題假設您已從 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 類別的現有相依性屬性消費者角度了解相依性屬性,並已閱讀相依性屬性概觀主題。This topic assumes that you understand dependency properties from the perspective of a consumer of existing dependency properties on Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) classes, and have read the Dependency Properties Overview topic. 為了解本主題中的範例,您也應該了解 Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) (請參閱 XAML 概觀 (WPF)) 並知道如何撰寫 WPFWPF 應用程式。In order to follow the examples in this topic, you should also understand Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) (see XAML Overview (WPF)) and know how to write WPFWPF applications.

物件存留期事件Object Lifetime Events

Microsoft .NET Framework managed 程式碼中的所有物件都經過一組類似的生命週期,例如建立、使用及銷毀。All objects in Microsoft .NET Framework managed code go through a similar set of stages of life, creation, use, and destruction. 許多物件的生命最終階段也會發生在解構階段,成為此階段的一部分。Many objects also have a finalization stage of life that occurs as part of the destruction phase. WPFWPF 物件,更確切的說就是 WPFWPF 識別為項目的視覺物件,也有一系列常見的物件生命階段。objects, more specifically the visual objects that WPFWPF identifies as elements, also have a set of common stages of object life. WPFWPF 程式設計和應用程式模型會將這些階段公開為一系列的事件。The WPFWPF programming and application models expose these stages as a series of events. 關於存留期事件,WPFWPF 中有四種主要的物件類型:一般項目、視窗項目、巡覽裝載和應用程式物件。There are four main types of objects in WPFWPF with respect to lifetime events; elements in general, window elements, navigation hosts, and application objects. 視窗和巡覽裝載也在較大的群組視覺物件 (項目) 內。Windows and navigation hosts are also within the larger grouping of visual objects (elements). 本主題說明通用於所有項目的存留期事件,再介紹適用於應用程式定義、視窗或巡覽裝載的較特定存留期事件。This topic describes the lifetime events that are common to all elements and then introduces the more specific ones that apply to application definitions, windows or navigation hosts.

項目共通的存留期事件Common Lifetime Events for Elements

任何 WPF 架構層級元素(衍生自 FrameworkElementFrameworkContentElement的物件)都有三個常見的存留期事件: InitializedLoadedUnloadedAny WPF framework-level element (those objects deriving from either FrameworkElement or FrameworkContentElement) has three common lifetime events: Initialized, Loaded, and Unloaded.


會先引發 Initialized,且大致對應于呼叫其函式的初始化物件。Initialized is raised first, and roughly corresponds to the initialization of the object by the call to its constructor. 因為事件是回應初始化而發生,所以保證設定物件的所有屬性。Because the event happens in response to initialization, you are guaranteed that all properties of the object are set. (例外狀況是運算式使用方式,例如動態資源或系結,這些會是未評估的運算式)。由於設定所有屬性的需求,因此,在標記中定義的嵌套元素所引發的 Initialized 順序,會依專案樹狀結構中最深層的元素順序出現,然後指向根項目的上層專案。(An exception is expression usages such as dynamic resources or binding; these will be unevaluated expressions.) As a consequence of the requirement that all properties are set, the sequence of Initialized being raised by nested elements that are defined in markup appears to occur in order of deepest elements in the element tree first, then parent elements toward the root. 此順序是因為父子式關聯性和內含項目都是屬性,因此在填滿屬性的子項目也完全初始化之前,父代無法報告初始化。This order is because the parent-child relationships and containment are properties, and therefore the parent cannot report initialization until the child elements that fill the property are also completely initialized.

當您撰寫處理常式以回應 Initialized 事件時,您必須考慮專案樹狀結構中的所有其他元素(邏輯樹狀結構或視覺化樹狀結構)都已建立,特別是父系要素.When you are writing handlers in response to the Initialized event, you must consider that there is no guarantee that all other elements in the element tree (either logical tree or visual tree) around where the handler is attached have been created, particularly parent elements. 成員變數可能是 Null,或資料來源可能未填入基礎繫結 (即使在運算式層級)。Member variables may be null, or data sources might not yet be populated by the underlying binding (even at the expression level).


接下來會引發 LoadedLoaded is raised next. Loaded 事件會在最終轉譯之前引發,但是在版面配置系統已計算出所有必要的值以進行轉譯之後。The Loaded event is raised before the final rendering, but after the layout system has calculated all necessary values for rendering. Loaded 需要包含專案的邏輯樹狀結構已完成,並連接到提供 HWND 和轉譯介面的呈現來源。Loaded entails that the logical tree that an element is contained within is complete, and connects to a presentation source that provides the HWND and the rendering surface. Loaded之前,會先發生標準資料系結(系結至本機來源,例如其他屬性或直接定義的資料來源)。Standard data binding (binding to local sources, such as other properties or directly defined data sources) will have occurred prior to Loaded. 非同步資料繫結 (外部或動態來源) 可能已發生,但根據其非同步性質的定義不能保證其已發生。Asynchronous data binding (external or dynamic sources) might have occurred, but by definition of its asynchronous nature cannot be guaranteed to have occurred.

引發 Loaded 事件的機制與 Initialized不同。The mechanism by which the Loaded event is raised is different than Initialized. Initialized 事件是依專案引發元素,而不需要由完成的專案樹狀結構直接協調。The Initialized event is raised element by element, without a direct coordination by a completed element tree. 相較之下,Loaded 事件會以協調的方式引發在整個專案樹狀結構中(特別是邏輯樹狀結構)。By contrast, the Loaded event is raised as a coordinated effort throughout the entire element tree (specifically, the logical tree). 當樹狀結構中的所有元素都處於被視為已載入的狀態時,就會先在根項目上引發 Loaded 事件。When all elements in the tree are in a state where they are considered loaded, the Loaded event is first raised on the root element. 然後,Loaded 事件會在每個子專案上連續引發。The Loaded event is then raised successively on each child element.


此行為表面上可能類似路由事件的通道。This behavior might superficially resemble tunneling for a routed event. 但是事件與事件之間不傳遞任何資訊。However, no information is carried from event to event. 每個元素都有機會處理其 Loaded 事件,並將事件資料標示為已處理,不會影響該元素。Each element always has the opportunity to handle its Loaded event, and marking the event data as handled has no effect beyond that element.


Unloaded 是最後引發,而且是由呈現來源或要移除的視覺父代所起始。Unloaded is raised last and is initiated by either the presentation source or the visual parent being removed. Unloaded 引發和處理時,屬於事件來源父系的專案(由 Parent 屬性決定)或在邏輯或視覺樹狀結構中向上或任何指定的專案,可能已經取消設定,表示資料系結、資源參考和樣式不得設定為其一般或上次已知的運行時間值。When Unloaded is raised and handled, the element that is the event source parent (as determined by Parent property) or any given element upwards in the logical or visual trees may have already been unset, meaning that data binding, resource references, and styles may not be set to their normal or last known run-time value.

存留期事件應用程式模型項目Lifetime Events Application Model Elements

以元素的常見存留期事件為基礎,是下列應用程式模型元素: ApplicationWindowPageNavigationWindowFrameBuilding on the common lifetime events for elements are the following application model elements: Application, Window, Page, NavigationWindow, and Frame. 這些會延伸共通存留期事件及與其特定用途相關的其他事件。These extend the common lifetime events with additional events that are relevant to their specific purpose. 下列位置對這些有詳細討論:These are discussed in detail in the following locations:

請參閱See also