Assembly: System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.
This event uses the RoutedEventHandler delegate type, but the event does not route (as many Windows Phone input events do). However, the nature of XAML loading of the object tree typically shows a pattern that superficially resembles a bubbling routed event pattern: all properties must be set on an object before it can be loaded. The deepest nested objects, such as objects that have no child objects, are loaded first—after all their XAML properties have values. For objects that have child objects, one of those properties is the property that holds the child objects. Therefore, the sequence of raising events occurs in an upward direction as the object tree is loaded. However, this sequence is really each object raising its own event in that order. Event data is not shared for the various events; these are all separate events, each raised by the individual object source.
There are exceptions to the above statement that events are raised in inverse tree order. Therefore, do not rely entirely on order for application logic. The following are some example cases where order can vary:
In most cases, the objects that are created from an applied control template or data template are the last objects to raise . However, this order principle can vary if specific controls use ApplyTemplate in initialization to force an early template load.
Because of template application and also because of data presentation rules for items in ItemsControl, sometimes the items will raise after their parent ItemsControl does, and after other branches of the main (non-templated) object tree raise .
Being resized by layout does not typically reload the object. See SizeChanged.
Loaded and Control Object Lifetime
In Windows Phone, the event is not guaranteed to occur after the template is applied. This might be an issue for you if you are using the event for a relatively common control scenario: you want to examine the visual tree, either to get a value as a source for something else or to change a value in the templated composition where you can know the new value only at run time. In this case, calls to the Windows Phone VisualTreeHelper methods to examine the visual tree of the template content might not work if you make them directly from a handler.
There are several ways to work around this issue. Each has merits as well as possible limitations:
If you are deriving from an existing control, instead of adding handling for , you can override OnApplyTemplate. OnApplyTemplate is specifically intended as the callback for this situation, where you have a tree from the template and now you want to examine or adjust the visuals. A limitation is that if you are just using an existing control as part of your application, you cannot change anything about OnApplyTemplate.
You can still continue to use . However, as the very first call in your handler, call ApplyTemplate on that control. ApplyTemplate is a synchronous method, so once you make that call and it returns, your template-created visual tree is now present. Calling ApplyTemplate in this case does not duplicate effort on the part of the Windows Phone runtime. A limitation is that your element of concern does need to be a Control derived class.
You can handle LayoutUpdated instead of . LayoutUpdated is the last "object lifetime" event in the sequence of enabling a control in Windows Phone UI. The main limitation of LayoutUpdated is that the initialization is possibly not the only time that LayoutUpdated is raised. Also, LayoutUpdated is raised for objects that are involved in a layout change. For instance, a peer in layout might have changed its size. In terms of visual trees, even the peer object may have changed only a few property values and not the tree structure, and the visual tree of the object that you are handling LayoutUpdated on might not have changed at all. Therefore, you might have to apply your own logic to determine whether a LayoutUpdated event really means that you need to examine or reexamine a visual tree.
Note that there are scenarios for handling that are not affected by the timing issues of template application. For instance, you can still add event handlers or set properties for the object where is raised, even if you cannot yet access the template parts. For instance, you can create objects in code and add them into content properties or content collections at this time, or add input-type event handlers that you chose not to hook up in the initial XAML.