Share via

RemoveEventListener Method

Unregisters an event listener on the event target object.

Cannot use methods in XAML.
object.RemoveEventListener(eventName, token)




The type of 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).



A token that is returned from an AddEventListener call. If you have no token, and the handler was added in XAML, you can pass 0.



The name of the event handler function. This syntax only works in two cases: either the handler was added in XAML, or the original AddEventListener call that added the handler used a quoted string rather than a delegate reference.


The AddEventListener method returns a token. Often you do not need this token, so you can call this method without storing the return value. However, if you plan to remove the event handler at some later point in your code, and you have added multiple listeners to the same event using scripting, you should retain this token.

The alternative AddEventListener syntax that specifies the event handler as a quoted string was originally for Safari browsers, which did not have the ability in the browser object model to retain handler references. You can still use the quoted string syntax as an option; the quoted string syntax effectively parallels the mechanism that is used to add the handlers that are declared as XAML attributes. When you use the quoted string syntax, you are not obligated to use the token in calls to RemoveEventListener. Instead of the retained token, you can specify the second parameter of RemoveEventListener as a quoted string of the handler name. To explicitly remove handlers that were added through XAML, you can either specify the handler name as a string, or use a token value of 0 (handlers from XAML always have that token value).

Use the RemoveEventListener method to remove a handler that has been added using the AddEventListener method.

Note   The Silverlight implementation of RemoveEventListener is different from the HTML Document Object Model (DOM) version method of the same name. The DOM version provides an additional third parameter that enables event capture.

Another important difference from the HTML DOM is that the the Silverlight implementation of AddEventListener will register multiple identical event listeners if provided. The event listener is called as many times as it was added. If there are multiples, a RemoveEventListener call removes only one of the instances, not all of them.


The following JavaScript example shows how to remove events from a TextBlock object, using tokens that were obtained from earlier AddEventListener calls:

function removeEvents()
    textBlock.removeEventListener("MouseEnter", entertoken1);
    textBlock.removeEventListener("MouseLeave", leavetoken1);

Applies To

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

See Also

Silverlight Events