Permalink
Fetching contributors…
Cannot retrieve contributors at this time
90 lines (73 sloc) 8.7 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
Object Lifetime Events
03/30/2017
.net-framework
dotnet-wpf
article
events, ContentRendered
events, Deactivated
events, Unloaded
Activated events
events, Loaded
Application objects, lifetime events
events, Activated
ContentRendered events
NavigationWindow class
Deactivated events
classes, NavigationWindow
Frame class
events, Initialized
events, Closing
Unloaded events
exit events
objects' lifetime events
Loaded events
Closing events
events, Closed
Initialized events
Closed events
classes, Frame
startup events
lifetime events of objects
face6fc7-465b-4502-bfe5-e88d2e729a78
19
dotnet-bot
dotnetcontent
wpickett

Object Lifetime Events

This topic describes the specific [!INCLUDETLA2#tla_winclient] events that signify stages in an object lifetime of creation, use, and destruction.

Prerequisites

This topic assumes that you understand dependency properties from the perspective of a consumer of existing dependency properties on [!INCLUDETLA#tla_winclient] classes, and have read the Dependency Properties Overview topic. In order to follow the examples in this topic, you should also understand [!INCLUDETLA#tla_xaml] (see XAML Overview (WPF)) and know how to write [!INCLUDETLA2#tla_winclient] applications.

Object Lifetime Events

All objects in [!INCLUDETLA#tla_netframewk] 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. [!INCLUDETLA2#tla_winclient] objects, more specifically the visual objects that [!INCLUDETLA2#tla_winclient] identifies as elements, also have a set of common stages of object life. The [!INCLUDETLA2#tla_winclient] programming and application models expose these stages as a series of events. There are four main types of objects in [!INCLUDETLA2#tla_winclient] 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

Any WPF framework-level element (those objects deriving from either xref:System.Windows.FrameworkElement or xref:System.Windows.FrameworkContentElement) has three common lifetime events: xref:System.Windows.FrameworkElement.Initialized, xref:System.Windows.FrameworkElement.Loaded, and xref:System.Windows.FrameworkElement.Unloaded.

Initialized

xref:System.Windows.FrameworkElement.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. (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 xref:System.Windows.FrameworkElement.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.

When you are writing handlers in response to the xref:System.Windows.FrameworkElement.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. Member variables may be null, or data sources might not yet be populated by the underlying binding (even at the expression level).

Loaded

xref:System.Windows.FrameworkElement.Loaded is raised next. The xref:System.Windows.FrameworkElement.Loaded event is raised before the final rendering, but after the layout system has calculated all necessary values for rendering. xref:System.Windows.FrameworkElement.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. Standard data binding (binding to local sources, such as other properties or directly defined data sources) will have occurred prior to xref:System.Windows.FrameworkElement.Loaded. Asynchronous data binding (external or dynamic sources) might have occurred, but by definition of its asynchronous nature cannot be guaranteed to have occurred.

The mechanism by which the xref:System.Windows.FrameworkElement.Loaded event is raised is different than xref:System.Windows.FrameworkElement.Initialized. The xref:System.Windows.FrameworkElement.Initialized event is raised element by element, without a direct coordination by a completed element tree. By contrast, the xref:System.Windows.FrameworkElement.Loaded event is raised as a coordinated effort throughout the entire element tree (specifically, the logical tree). When all elements in the tree are in a state where they are considered loaded, the xref:System.Windows.FrameworkElement.Loaded event is first raised on the root element. The xref:System.Windows.FrameworkElement.Loaded event is then raised successively on each child element.

[!NOTE] This behavior might superficially resemble tunneling for a routed event. However, no information is carried from event to event. Each element always has the opportunity to handle its xref:System.Windows.FrameworkElement.Loaded event, and marking the event data as handled has no effect beyond that element.

Unloaded

xref:System.Windows.FrameworkElement.Unloaded is raised last and is initiated by either the presentation source or the visual parent being removed. When xref:System.Windows.FrameworkElement.Unloaded is raised and handled, the element that is the event source parent (as determined by xref:System.Windows.FrameworkElement.Parent%2A 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

Building on the common lifetime events for elements are the following application model elements: xref:System.Windows.Application, xref:System.Windows.Window, xref:System.Windows.Controls.Page, xref:System.Windows.Navigation.NavigationWindow, and xref:System.Windows.Controls.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

Dependency Property Value Precedence
Routed Events Overview