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.
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.
Add <TargetPlatformVersion>8.0</TargetPlatformVersion> to <PropertyGroup> so that it looks like the following:
Then reload the project, and select Add Reference from the project’s References node context menu.
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.
Here’s an example of using a WinRT API in a desktop application.
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:
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:
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.
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.
You mention GPS APIs as being available for Desktop, but according to MSDN the Geolocator class is for Windows Store apps only. Is there another API you were referring to?
Great post! But I'm looking at the MSDN documentation for Geolocator and it doesn't appear to support desktop apps. Is there another GPS API you are referring to?
According to the documentation of the Geolocator API it is only available for Store apps, not desktop apps. Is their a different WinRT GPS API that is available to desktop?
Windows 8 documentation is still in progress and for some classes information about availability on Desktop is not complete. Geolocator class is available for Desktop. For example, the following code works without any issues:
Geolocator locator = new Geolocator();
var position = await locator.GetGeopositionAsync();
In general, mainly UI-related classes and classes tightly coupled with WinRT app lifecycle are not available for desktop applications.
Simply, you can test Geolocator desktop-console applicaiton using Windows 8 simulator.
In command-line, run simulator as follow
"C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Simulator\11.0\Microsoft.Windows.Simulator.exe" /LCID 1033
Then you'll see the applicaiton with Geolocator is working in the simulator.
Is there a way to do this in vs2013 RC ?
Absolutely! All provided information about accessing WinRT is applicable for VS 2012 and VS 2013 RC.
Hi. Great post. Thanks.
You know what it is System.Runtime.WindowsRuntime.UI.Xaml.dll?
This appears in the same folder. I have intrigued.
Starting from Windows 8 XAML is part of system stack. Therefore, when we develop modern apps using C#/XAML we actually use WinRT XAML re-built using C++. System.Runtime.WindowsRuntime.UI.Xaml.dll provides mapping between C# and some of WinRT UI classes.
Can WinRT APIs being used in C++ console app? or from unmanaged native C++ components?
WinRT is based on COM so you should be able to access it from desktop C++ app. Please take a look the following articles:
1) Windows Runtime C++ Template Library (WRL)
2) How to: Activate and Use a Windows Runtime Component Using WRL
Thank you for your shining example codings.
Which steps would be necessary to run the executable on a win7 machine ?
@Edmond de Savornin Lohman,
WinRT is an integral part of Windows 8 (and 8.1) and if your application uses WinRT APIs, it requires Windows 8 or later to run.
Is there anyway to get data out of the magnetometer in a desktop application? According to MSDN the magnetometer class is for Windows Store apps only.
Magnetometer class is only available for phone platform, even in Windows Store apps you cannot read read raw magnetometer data. Instead you can take a look Sensor API - msdn.microsoft.com/.../dd318953%28v=vs.85%29.aspx