To use a Windows API, you must first require in the class, then you can invoke its APIs. For most APIs, you can directly use the API name without any modification.
Before using an API make sure it can be supported first. Open the API reference for the API you want to use.
- Check if it is supported in C++. In the Syntax section at the top of the API reference, the tabbed group should contain a C++ tab.
- Check that the minimum API version is at least Windows 8 or 8.1. At the bottom of the API reference, check for a Requirements (Windows 8.x and Windows Phone) section.
- Check if the API supports only Windows Phone or only Windows. Some APIs may only support one platform. Check the platform in the application before invoking the API.
Require a class
To include a WinRT C++ class, pass the fully qualified class name to the
require() method. Do not pass variables or concatenated strings that evaluate to the class name to the
require() call. The Titanium SDK looks specifically for any string literals that start with
Windows. in order to include the class when building the application.
After requiring in the class, you can invoke API calls:
Enumerations in WinRT are exposed automatically for you when you require any API that may use that enumeration. For example, the
Windows.UI.Xaml.HorizontalAlignment enum values will automatically be included when you require
removeEventListener() methods rather than the C++ syntax.
In C++, you would create an event handler and assign it to an event property of a Windows object or class.
addEventListener() method on the Windows object or class, and pass the event name and event handler to the method.
Async return types
Some of the WinRT APIs return asynchronous return types, such as
After invoking the method, chain either the
then(fulfilledFunction, rejectedFunction) or
catch(rejectedFunction) method to the invoked WinRT method to handle the response after the operation completes.
Mix WinRT APIs with Titanium APIs
You can mix and match WinRT and Titanium APIs together. You can call Titanium APIs from a native event callback, or call Native APIs inside a callback registered with Titanium APIs.
You can also mix and match Titanium.UI.View and its subclasses with any WinRT API expecting a
Windows.UI.Xaml.UIElement. For example, you can create a Titanium Window or View object, then add a native UI element, such as a Tooltip, which the Titanium SDK has equivalent API for, to it. Or you can generate a complex layout using the WinRT APIs, then add Titanium Labels, Buttons or Views to it.