次の方法で共有


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.

Comments

  • Anonymous
    October 03, 2013
    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?

  • Anonymous
    October 03, 2013
    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?

  • Anonymous
    October 03, 2013
    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?

  • Anonymous
    October 03, 2013
    The comment has been removed

  • Anonymous
    October 03, 2013
    The comment has been removed

  • Anonymous
    October 08, 2013
    Is there a way to do this in vs2013 RC ?

  • Anonymous
    October 08, 2013
    @Console, Absolutely! All provided information about accessing WinRT is applicable for VS 2012 and VS 2013 RC.

  • Anonymous
    October 27, 2013
    Hi. Great post. Thanks. You know what it is System.Runtime.WindowsRuntime.UI.Xaml.dll? This appears in the same folder. I have intrigued.

  • Anonymous
    October 28, 2013
    @pedrozopayares, 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.

  • Anonymous
    December 26, 2013
    The comment has been removed

  • Anonymous
    December 27, 2013
    The comment has been removed

  • Anonymous
    March 03, 2014
    Thank you for your shining example codings. Which steps would be necessary to run the executable on a win7 machine ?

  • Anonymous
    March 03, 2014
    @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.

  • Anonymous
    May 28, 2014
    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.

  • Anonymous
    June 07, 2014
    @John, 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

  • Anonymous
    July 05, 2015
    Do you know how can Desktop App (Win32) reference a new WinRT API that only support Windows 10?