Using Windows 8 WinRT APIs in .NET Desktop Applications

WinRT is a set of modern, object-oriented APIs introduced in Windows 8. WinRT is built on top of Win32 and COM and is designed to provide interoperability for the various languages using metadata. In addition to the previously available Windows and .NET APIs, now exposed in a cleaner, streamlined way, WinRT provides an access to all the new features of Windows 8.

Some WinRT APIs are closely coupled with the new Windows Store app model while others can safely be used for desktop application development. For Windows desktop applications, using the WinRT APIs enables simple ways to access GPS and NFC devices, the accelerometer, orientation, light, and other sensors. Moreover, some WinRT classes are only available for desktop apps, like PackageManager.

Accessing WinRT APIs from desktop applications is a supported process and the documentation on how to do it is light. But you are in the right spot because that is what I will cover here. For example, the following screenshot demonstrates availability of Windows.Globalization.Calendar class for both Windows Store and desktop apps.

image

Leveraging WinRT APIs from Desktop Applications

To gain access to the features enabled by the WinRT APIs, a desktop application has to have Windows 8 as its target platform. Currently Visual Studio does not provide a UI for setting the target platform, so to modify the platform you’ll need to unload the target project in Visual Studio by right-mouse clicking on the project name and selecting Unload Project, and then edit the project file.

image

Add <TargetPlatformVersion>8.0</TargetPlatformVersion> to <PropertyGroup> so that it looks like the following:

image

Then reload the project, and select Add Reference from the project’s References node context menu.

image

The Windows library will allow your desktop application to use the core features of Windows 8.

One more peace is required to enable mappings between .NET Framework types and WinRT types. They are required, for example, to handle WinRT events and async methods. To add the mapping, open Reference Manager again and manually add System.Runtime.WindowsRuntime.dll located in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5

Now you can easily use WinRT APIs in your desktop application.

Example: Using the Accelerometer

Here’s an example of using a WinRT API in a desktop application.

code01

 image

Example: Accessing user libraries and using search queries

WinRT provides easy access to the metadata of files and allows using it in file search queries. As an example, you can use the following code to get all the photos taken using a Pentax camera and saved as .jpg files into the Pictures library:

code02

image

Example: Identification of Available Input devices

Microsoft Office 2013 applications are a good example of how desktop applications can be optimized for supporting touch-enabled systems. When an Office application detects that system is equipped with a touch device, such as tablet or Ultrabook touch screen, it displays additional controls:

image

If no mouse is connected to tablet, the application switches UI to the touch-optimized mode. In contrast, on Ultrabooks, for example, that have trackpads, Word stays in mouse-centric mode, allowing user to enable touch mode manually.

The following WinRT code demonstrates detection of the available input devices and can be used as a first-in-the-adaptive-UI implementation.

code03

Other WinRT features

The samples in this post demonstrate just a small part of what’s available from WinRT for desktop applications. Ambient Light sensor, GPS, NFC, PasswordVault and many other WinRT APIs will let you use available hardware effectively and will enhance functionality of your desktop applications.

As you can see, WinRT in Windows 8 does a good job of enabling developers of both Windows Store style apps, as well as desktop apps, to take advantage of all of the innovations that come with Windows 8-based machines in just a few lines of code.