Registers an event listener on the event target object.
| XAML | Cannot use methods in XAML. |
| Scripting |
[token = ]object.AddEventListener(eventName,
functionReference)
|
Parameters
| eventName | string The name of the event, such as "KeyDown". This is a true string, and the value must be quoted. An exception is thrown if eventName is invalid (not a known event name). |
| functionReference | object A reference to the event handler function. Specifying the name of an existing function (without quotes) will create a delegate that fills this object reference. -or- The name of an existing function, with quotes. See Remarks. |
Return Value
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.
Examples
The following JavaScript example shows how to define a Silverlight keyboard event
by using the AddEventListener method:
| JavaScript |
function onLoaded(sender, eventArgs)
{
// Set the root Canvas object to a KeyDown event handler function.
var token = sender.addEventListener("keyDown", onKeyDown);
}
function onKeyDown(sender, keyEventArgs)
{
// Determine whether the keystroke combination CTRL+V was detected.
if ((keyEventArgs.key == 51) && (keyEventArgs.ctrl == true))
{
// Retrieve a reference to the plug-in.
var slPlugin = sender.getHost();
// Display the current version of the plug-in.
alert("Silverlight version: " + slPlugin.isVersionSupported("1.0"));
}
}
|
The following Javascript example shows how to
remove a Silverlight event handler function for the KeyDown event. The token variable in the previous example is used here to fill the second parameter of the RemoveEventListener
method.
| JavaScript |
function onCleanUp()
{
// Remove the KeyDown event handler function from the root Canvas object.
sender.removeEventListener("keyDown", token);
}
|
Applies To
Canvas,
Ellipse,
Glyphs,
Image,
InkPresenter,
Line,
MediaElement,
Path,
Polygon,
Polyline,
Rectangle,
TextBlock
See Also
Silverlight Events
RemoveEventListener