April 30, 2019 3:30 pm

Calling Windows 10 APIs From a Desktop Application just got easier

By (Program Manager, Windows Developer Team) and

Today, we are pleased to announce we posted on nuget.org a preview of the Windows 10 WinRT API Pack. By using these NuGet packages, you can quickly and easily add new Windows functionality to your applications like Geolocation, Windows AI, Machine Learning, and much more. 

We have posted 3 packages: 

Each package includes all of the Windows Runtime (WinRT) APIs included with each specific Windows release. These are preview packages, so please give us feedback and watch for updates to the known issues on our repository.    

How can this help me? 

Previously, in order to access the Windows API surface from your WPF or Winforms app, you needed to specifically add contract files and other reference assemblies to your project. With this release, you can simply add a NuGet package and we will do the heavy lifting to add the contracts. 

In addition, when using the NuGet packages, updating to the latest Windows Runtime (WinRT) APIs in your project will be as simple as checking for an update to the NuGet package.   

Getting Started  

Step 1: Configure your project to support Package Reference  

Step 2: Add the Microsoft.Windows.SDK.Contracts NuGet package to your project  

  1. Open the NuGet Package Manager Console  
  2. Install the package that includes the Windows 10 Contracts you want to target. Currently the following are supported:  

Windows 10 version 1803 

Install-Package Microsoft.Windows.SDK.Contracts -Version 10.0.17134.1000-preview  

Windows 10 version 1809 

Install-Package Microsoft.Windows.SDK.Contracts -Version 10.0.17763.1000-preview  

Windows 10 version 1903 

Install-Package Microsoft.Windows.SDK.Contracts -Version 10.0.18362.2002-preview 

Step 3: Get coding 

By adding one of the above NuGet packages, you now have access to calling the Windows Runtime (WinRT) APIs in your project.   

For example, this snippet shows a WPF Message box displaying the latitude and longitude coordinates: 


private async void Button_Click(object sender, RoutedEventArgs e) 
{ 
    var locator = new Windows.Devices.Geolocation.Geolocator(); 
    var location = await locator.GetGeopositionAsync(); 
    var position = location.Coordinate.Point.Position; 
    var latlong = string.Format("lat:{0}, long:{1}", position.Latitude, position.Longitude); 
    var result = MessageBox.Show(latlong); 
} 

Adaptive code for previous OS 

Each package includes all the supported Windows Runtime APIs up to Windows 10 version of the package.  If you are targeting earlier platforms, consider only offering functionality available on the detected platform version.  For further details, see the following article: Version adaptive code. 

For additional information on calling Windows 10 APIs in your desktop application, please see the following article: Enhance your desktop application for Windows 10. 

If you want to check out a project that is already using these NuGet packages, see: https://github.com/windows-toolkit/Microsoft.Toolkit.Win32.