This event uses the RoutedEventHandler delegate type, but the event does not bubble (as many Silverlight input events do). However, the nature of XAML loading of the object tree will typically show a pattern that superficially resembles a bubbling routed event pattern: all properties must be set on an object before it can be loaded. The most deeply nested objects with no children are loaded first, once all their XAML properties have values. For objects with child objects, one of those properties is the property that holds the children. Thus the sequence of Loaded events firing will occur in an upwards direction as the object tree is loaded, but this is really each object firing its own Loaded event in that order. Event data is not shared for the various Loaded events; these are all separate events each raised by the individual object source.
Note: |
|---|
There are some specific exceptions to the above statement that Loaded events are raised in inverse tree order. For instance, because of template application and data presentation rules for items in ItemsControl, sometimes these will raise Loaded after their parent ItemsControl does. Therefore, do not exclusively rely on Loaded order for overall application logic. |
Silverlight does not have an equivalent event for the WPF event Initialized.
Being resized by layout does not typically reload the object. See SizeChanged.
Loaded and Control Object Lifetime
The timing of the Loaded event in Silverlight differs from the timing of the FrameworkElement.Loaded event in WPF. Specifically, the WPF Loaded event occurs after the template is applied. In Silverlight, the Loaded event is not guaranteed to occur after the template is applied. This might be an issue for you if you are using the Loaded 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 Silverlight VisualTreeHelper methods to examine the visual tree of the template content might not work if you make them directly from a Loaded 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, rather than adding handling for Loaded, 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 consuming an existing control as part of your application, you cannot change anything about OnApplyTemplate.
You can still continue to use Loaded. However, as the very first call in your Loaded 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 Silverlight runtime. A limitation is that your element of concern does need to be a Control as rather than a FrameworkElement.
You can handle LayoutUpdated instead of Loaded. LayoutUpdated is the last “object lifetime” event in the sequence of enabling a control in Silverlight 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 Loaded 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 Loaded 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.