Condividi tramite


Loaded

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Arguments

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, quotation marks around the function name are not required. (See the "Remarks" section.)

Event Handler Parameters

sender

object

The object that invoked the event.

eventArgs

object

This parameter is always set to null.

Managed Equivalent

Loaded

Remarks

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

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

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 occurs before the parent object's Loaded event. This is because the page root cannot consider itself loaded until all its constituent parts are loaded.

Certain properties have an implicit or explicit asynchronous behavior and do not block the Loaded event. For example, if you use a Downloader object to set an image source, the image will initially load with no source and Loaded will still occur. After the asynchronous download is completed, the associated Downloader will raise the Completed event.

Caution noteCaution:

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

Example

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.

<!-- TextBlock Loaded event occurs first, then Canvas Loaded event -->
<Canvas
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://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, because it is not referenced by the function.

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 and the browser Document Object Model (DOM) provide a set of events that you can use to respond to changes in the Web page, including the similarly named 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 browser DOM onload event:

<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 raised until the entire Web page is loaded. This means that any Silverlight plug-in contained within the page will raise its Loaded event before the JavaScript onload event.

Applies To

Border (Silverlight 2)

Canvas

Ellipse

Glyphs

Image

InkPresenter

Line

MediaElement

MultiScaleImage (Silverlight 2)

PasswordBox (Silverlight 2)

Path

Polygon

Polyline

Popup (Silverlight 2)

Rectangle

StackPanel (Silverlight 2)

TextBlock

TextBox (Silverlight 2)

See Also

Reference