Using WinRT from .NET

As it turns out, it is possible to use the WinRT APIs from standard .NET. You’ll need the full Visual Studio 2011 preview installed on the Windows 8 Developer Preview.

First, create a new WPF project. Then, go to add references and press “browse”. Navigate to C:\Program Files (x86)\Windows Kits\8.0\Windows Metadata

C:\Program Files (x86)\Windows Kits\8.0\Windows Metadata

Make sure you select “All Files”. Note that VS11 gives you an error if you try to add more than one at a time, so you’ll have to add each individually. We’re going to show a quick app that lists devices, so we’ll need a couple of these namespaces: windows.foundation.winmd and windows.devices.enumeration.

First we’ll make a little view model:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Foundation;

namespace WpfApplication1
{
    public class MainWindowViewModel
        : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public MainWindowViewModel()
        {
            var operation = DeviceInformation.FindAllAsync();
            operation.Completed = CompletedFindDevices;
            operation.Start();
        }

        public IEnumerable Devices
        {
            get { return this.devices; }
            private set
            {
                if (this.devices == value)
                    return;

                this.devices = value;
                OnPropertyChanged ("Devices");
            }
        }

        private IEnumerable devices;

        private void OnPropertyChanged (string propertyName)
        {
            var changed = PropertyChanged;
            if (changed != null)
                changed (this, new PropertyChangedEventArgs (propertyName));
        }

        private void CompletedFindDevices (IAsyncOperation asyncInfo)
        {
            Devices = asyncInfo.GetResults();
        }
    }
}

And then the Xaml (don’t forget to hook up the datacontext to the view model in your code behind):

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <ListBox ItemsSource="{Binding Devices}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>

And voila:

This doesn’t cover being able to implement the various platform contracts, but it’s a start.

Update: Apparently the opposite is true as well.. kind of. It’s possible to reference your existing .NET assemblies and use them in a WinRT application. It remains to be seen or heard whether that will be allowed for the App Store, but it does work in practice. The gotcha is that you can’t reference the missing pieces of the BCL, so if you need a type there (System.Net.EndPoint for example), you’re still out of luck.

Update 2: The app certification utility fails WinRT apps calling unsupported .NET APIs. So, they’ll run, but you won’t get them on the store.

This entry was posted in WinRT and tagged , . Bookmark the permalink.

5 Responses to Using WinRT from .NET

  1. Pingback: Build Windows Day 2 From Afar: WTF is WinRT | Jay R. Wren – lazy dawg evarlast

  2. Pingback: Windows 8 and WinRT: Links, News and Resources « Angel “Java” Lopez on Blog

  3. DavidG says:

    Thanks for the post. How do you suppose they are going to allow desktop apps into the market if they can’t pass the App Store certification step? Is there a different path to adding old apps to the App Store?

  4. ermau says:

    DavidG: The distinction is that Win32/.NET apps can be listed on the store, but they can’t be purchased or installed from it. That right is reserved for Metro style apps.

  5. Kevin says:

    Any thoughts on how to use WinRT to get back CPU serial # or some other machine-indepdendent unique ID?

    Thanks

Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>