Export (0) Print
Expand All

Handling Windows Runtime Events in JavaScript

Windows Runtime events are not represented in the same way in JavaScript as they are in C++ or the .NET Framework. They are not class properties, but rather are represented as string identifiers that are passed to the class's addEventListener and removeEventListener methods. For example, you can add an event handler for the Geolocator.PositionChanged event by passing the string "positionchanged" to the Geolocator.addEventListener method:

var locator =  new Windows.Devices.Geolocation.Geolocator();
locator.addEventListener(
    "positionchanged", 
     function (ev) {
        console.log("Got event");
    });

You can also set the locator.onpositionchanged property.

locator.onpositionchanged =  
    function (ev) {
        console.log("Got event");
    };

In JavaScript, Windows Runtime event arguments are represented as a single event object. In the following example of an event handler method, the ev parameter is an object that contains both the sender (the target property) and the other event arguments. The event arguments are the ones that are documented for each event.

function (ev) {
    console.log("Target: " + ev.target);
    console.log("Position: " +
        ev.position.latitude + "," +
        ev.position.longitude);
};
Important noteImportant

Windows Runtime features are not available for apps that run in Internet Explorer.

Show:
© 2014 Microsoft