Windows Runtime (WinRT)
What is WinRT?
The Windows Runtime (WinRT) is a platform-agnostic application architecture introduced in Windows 8. It provides a set of APIs that enable developers to create modern, high‑performance applications that run across Windows devices, from desktops to tablets and phones.
Key Concepts
- Language Projections: Use native language constructs (C++, C#, JavaScript, VB.NET) to access WinRT APIs.
- Asynchronous Programming: Most WinRT APIs are asynchronous, returning
IAsyncOperation
orIAsyncAction
objects. - Contracts: Feature groups (e.g.,
Windows.Storage
,Windows.UI.Xaml
) that define related APIs. - Metadata: WinRT components expose metadata in
.winmd
files, enabling tooling and IntelliSense.
Getting Started
- Install the latest Visual Studio with the Desktop development with C++ and Universal Windows Platform development workloads.
- Create a new Blank App (Universal Windows) project.
- Explore the default
MainPage.xaml
andMainPage.xaml.cs
files to see XAML UI and code‑behind. - Run the project on the local machine or an emulator.
Sample Code
Read a text file asynchronously using the Windows.Storage
APIs:
using Windows.Storage;
using System;
async void LoadFile()
{
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.GetFileAsync("sample.txt");
string content = await FileIO.ReadTextAsync(file);
System.Diagnostics.Debug.WriteLine(content);
}