Loaded Event

Occurs when the Silverlight content is loaded into the host Silverlight plug-in and parsed, but before the content is rendered.

XAML
<object Loaded="eventhandlerFunction" .../>
Scripting
[token = ]object.AddEventListener("Loaded", eventhandlerFunction)

AddEventListener Parameters

token

integer

A token that is returned from the function, which you can optionally retain as a variable. If you intend to call RemoveEventListener to remove the handler, you will need this token.

eventhandlerFunction

object

The name of your event handler function as it is defined in script. When used as an AddEventListener parameter, quotes around the function name are not required. See Remarks.

Event Handler Parameters

sender

object

Identifies the object that invoked the event.

eventArgs

object

This parameter is always set to null.

Remarks

The Loaded event can be defined for any UIElement-derived class, such as Canvas, TextBlock, or Rectangle.

You can also add handlers in script using a quoted string for the event handler name:

object.AddEventListener("Loaded", "eventhandlerFunction")

This syntax also returns a token; however, the token is not an absolute requirement for removing the handler, in cases where the handler was added by using a quoted string. For details, see RemoveEventListener.

You can define multiple Loaded events for objects in XAML content. However, if a child object and its parent object both define a Loaded event, the child object's Loaded event is fired before the parent object's Loaded event.

Caution   The alert() mechanism that is provided in JavaScript on browsers is not a reliable way to examine the order in which events are fired. Frequently the alerts will fall out of the true order of the events. Instead of using alert(), use the technique shown here and write out log messages to a plug-in on the page, or something similar.

Examples

The following XAML example shows Loaded events defined for a Canvas and TextBlock object. In this case, the TextBlock object's event handler function is invoked before the Canvas object's event handler function.

XAML
<!-- TextBlock Loaded event fires first, then Canvas Loaded event -->
<Canvas
  xmlns="http://schemas.microsoft.com/client/2007"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Loaded="rootCanvasLoaded">

  <TextBlock
   x:Name="myTextBlock" 
   Loaded="textBlockLoaded"
   Text="Test order of Loaded events" />

</Canvas>

The following JavaScript example shows how to implement a Loaded event handler function. Notice that the eventArgs parameter is omitted, since it is not referenced by the function:

JavaScript
function rootCanvasLoaded(sender)
{
    // Set the TextBlock to display the current date and time.
    sender.findName("myTextBlock").text = Date();
}

The Silverlight plug-in provides an OnLoad event that occurs after the XAML content in the Silverlight plug-in has completely loaded. This means that the Onload event always occurs after any Loaded events occur.

JavaScript provides a set of events that you can use to respond to changes in the Web page, including the onload event. You can use the onload event and the Silverlight Loaded event on the same page. The following HTML example shows how to use the onload event:

JavaScript
<body onload='javascript:alert("onload event generated")'>

In this example, JavaScript code is used directly instead of referencing an event handler function--this is not possible with Silverlight events. By definition, the JavaScript onload event is not fired until the entire Web page is loaded. This means that any Silverlight plug-in contained within the page will fire its Loaded event before the JavaScript onload event.

Applies To

Canvas, Ellipse, Glyphs, Image, InkPresenter, Line, MediaElement, Path, Polygon, Polyline, Rectangle, TextBlock

See Also

Silverlight Events
OnLoad

Community Additions

Show: