Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
From the author of WinRT

WinRT

The Windows Runtime itself provides many benefits to developers. Take a look at the following code:

public async void CaptureButton_Click(object sender, RoutedEventArgs e)
 {
     var camera = new CameraCaptureUI();
     var result = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo);
     if (result != null)
     {
         var fileStream = await result.OpenAsync(Windows.Storage.FileAccessMode.Read);
               _writeableBitmap = new WriteableBitmap(1, 1);
         _writeableBitmap.SetSource(fileStream);
         _writeableBitmap.Invalidate();
         ImageTarget.Source = _writeableBitmap;
     }
 }

Can you figure out what it does? In less than a dozen lines of the code, users are prompted for permission to use their web camera. If allowed, they will be presented with a full-screen view of video and the ability to switch between the front camera and the rear-facing camera if one is available. The user can tap the screen to take a picture, then either retake it or crop it, and select it. The image is returned and stored in a WriteableBitmap object to display with XAML.

Did you spot the unmanaged code in this example? The CameraCaptureUI object is actually a WinRT Component. It is unmanaged code in the operating system that does quite a bit behind the scenes to enable image and video capture. To a developer, it looks like a simple CLR object with methods to call. This is because WinRT uses a powerful feature called projection that makes native API calls look and feel familiar to the developer based on their programming language preference.

You can create your own WinRT objects using the language of your choice. Those components can then be used in projects written in other languages. To a C# program, it will look like a CLR object. To a JavaScript program, it will look like a JavaScript object. Under the covers, Windows 8 uses a combination of the decades-old Component Object Model (COM), the ECMA-335 standard for a Common Language Interface (CLI) to define metadata around API calls, and the Windows Registry to work some amazing magic for you.

  • + Share This
  • 🔖 Save To Your Account