Metro Application and Windows Runtime (WinRT)

Introduction

Windows Runtime (WinRT) is a set of APIs that can be used to build Metro-style applications. WinRT helps developers create applications that look intuitive for the application’s end users. A developer can use these WinRT APIs with multiple languages including C#, VB, JavaScript, and C++. These APIs are sets of metadata and .NET can reference these WinRT components directly. just like .NET assemblies. Developers just need to reference a .winmd file to access all the types and their members in its metadata just as with .NET objects.

How Metro-style Applications Work with Windows Runtime

The Windows Runtime uses API metadata (.winmd files) to expose its functionality. You can find all these Metadata files in your system path: C:WindowsSystem32WinMetadata. These are not the assemblies; all these Runtime APIs are native and are built in C++. You can see the metadata information of these APIs through ILDASM.exe as we see in .NET assembly’s metadata information.

Each .winmd file exposes one or more namespaces. These namespaces are grouped by the functionality that they provide. A namespace contains types such as classes, structures, and enumerations. A type contains members such as methods, properties, and fields.

APIs Used to Expose WindowsRuntime

Here is a list of APIs that use Metadata to expose Windows Runtime:

  • Windows.ApplicationModel: Manages app launch, activation, suspend, and resume
  • Windows.Data: Manages XML data
  • Windows.Devices: Provides support for devices, such as sensors and cameras
  • Windows.Foundation: Provides fundamental functionality, including reading and writing asynchronously, and managing property sets and collections
  • Windows.Graphics: Provides graphics support for applications
  • Windows.Media: Provides audio and video functionality
  • Windows.Networking: Provides networking functionality
  • Windows.Security: Provides access to security features
  • Windows.Storage: Manages files, folders, and app data
  • Windows.System: Provides access to system features
  • Windows.UI: Provides a framework and API to define the user interface
  • Windows.UI.Xaml: Provides a framework and API for using Extensible Application Markup Language (XAML) to define the user interface
  • Windows.Web: Enables you to manage syndication feeds and access resources using the AtomPub protocol

Programming Concepts of Windows Runtime

Activatable Classes

You can create an instance of a Windows Runtime class; this calls an activation command. Each programming language has its own way of activating classes. The lifetime of the objects of these classes is managed by Windows and it releases them when you are done.

Asynchronous Operation

Metro-style applications handle long-running processes without blocking the UI thread. They use the Windows Runtime asynchronous programming model to handle this. Tasks can be cancelled and also can show progress notifications when it runs in the background. A developer does not need to manage threads explicitly.

Capabilities

Metro-style allocations are restricted to accessing user data or system resources by default. The access to these pieces can be granted at run time with user input.

Example: If a Metro-style application is try to access the system’s webcam, a message will be populated to the user to grant access for the same.

Contracts

Metro-style applications can share information with other applications with the help of a share contract. Following is the contact list that can be used in Metro-style applications:

  • Search: Enables you to search your application from anywhere in Windows. Application provides search suggestions to help users find something very quickly.
  • Share: A share contract lets any application share data with any other application. A source application provides something to share and a target application is an application being shared through. It provides a set of common formats for data interchange.
  • PlayTo: Application can be connected to play audio, video, or images on a connected device.
  • Settings: Helps the user make the settings consistent with the Windows settings model.
  • Picker: A picker contract allows other applications to choose content from your application.

Custom Components

You can build reusable libraries for use with your apps as custom components. Build your custom components with C#, Visual Basic, or Microsoft Visual C++ and use them from JavaScript or any other supported language. Your custom components are included in the package for your app.

Advantages of Using Windows Runtime APIs

  • Prior to WinRT, Windows APIs were not accessible by CLR directly. Developers needed to write a bunch of interop code to access the functionality of these APIs. However, Windows Runtime APIs can be accessed through CLR and can be used in C# or VB.NET code directly without writing a separate code. To access the functionality of it, you just need to add the reference of that API in your code file as you do for other .NET libraries.
  • Developers can create Windows Runtime components in C# or VB.NET. These components can be used with JavaScript code, too.
  • Windows Runtime is designed in a way that a managed code developer can use them as he uses C# or VB.NET code.
  • Windows Libraries like Music, Pictures, Videos, Microphone, and Webcam easily can be accessed through Windows Runtime in Metro-style applications.

Here is a quick example that shows how easily a developer can use Windows Runtime APIs in their code. Developers do not need to write a separate block of code to access these windows APIs. The given code bock captures an image through the system webcam and stores it.

using Windows.Media.Capture;           // WinRT API
using Windows.UI.Xaml.Media.Imaging;   // WinRT API
try
{
   CameraCaptureUI imagedialog = new CameraCaptureUI();
   Size captureRatio = new Size(16, 9);
   imagedialog.PhotoSettings.CroppedAspectRatio = captureRatio;
   StorageFile storageFile = null;
   storageFile = await
   imagedialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
   if (storageFile != null)
   {
      Scenario1OutputText.Text = "";
      IRandomAccessStream ImageStream = await
      storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
      BitmapImage imagebitmap = new BitmapImage();
      imagebitmap.SetSource(ImageStream);
      Scenario1Image.Source = imagebitmap;
      Scenario1ResetButton.Visibility = Visibility.Visible;
      photoFile = storageFile.Path;
   }
   else
   {
      TextBlock1.Text = "Please capture a photo.";
   }
}
catch (Exception ex)
{
   TextBlock1.Text = ex.Message;<
}

Conclusion

Windows Runtime made developers’ lives easy to use Windows components directly in their applications, just as they use native APIs. Over time, the Windows Runtime will provide a fuller subset of Windows technologies to support engaging applications and great experiences.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read