#TIL Universal XAML Apps Are Neat

If you missed the Build conference a week or two ago, you probably didn’t hear about a bunch of cool stuff. One of the cooler things was the concept of a Universal App in the Windows ecosystem – you can now target both Windows 8.1 and Windows Phone 8.1 with any C#/C++/WinJS application you make.

Both Windows 8.1 and Windows Phone 8.1 share a common runtime (“WinRT”), and the VS team has finally released some tools to allow you to take advantage of that. Also as a part of this, you can now deploy WinJS applications to the phone, which is a big deal for existing JavaScript / HTML developers trying to make native applications. This blog post goes over everything in more detail, but here’s a quick summary.

First off you want to install Visual Studio Update 2 RC. This will install all the stuff you need to take advantage of this new feautre.

The basic structure of a universal app consists of a project for each target platform (Windows and Windows Phone), and one or more shared “portable class libraries”. This is the solution template you’ll get when you first create a universal app, but if you already have a Windows 8.1 app you can add a target platform for Windows Phone 8.1 by simply right-clicking the project and selecting “Add Windows Phone 8.1…”.

PCLs have been around for awhile, but if you’re unfamiliar they’re just libraries that can be compiled to a number of target platforms. The previous issue with PCLs was the fact that they were “lowest common denominator” – because you target multiple platforms, you could only access the parts of .NET that were common across all platforms. With this update, they’ve expanded the reach of PCLs to include the WinRT APIs, which now allow you to include UI components as well as have access to previously-unavailable core APIs like File.

As you can imagine, you can develop and deploy common UI controls in these shared PCLs, and reference them in your target platform projects. This allows you to do reusable UI work once and take advantage of it multiple times, hopefully cutting down the work involved. You can also share other resources, such as xaml files, xml files, PNG images, resource files, classes, and more. There’s also a few compiler directives included to make development easier if you need to include specific code for each platform in your shared libraries – just include #if WINDOWS_APP or #if WINDOWS_PHONE_APP.

There’s also a few improvements to visual studio to help make dual-development easier. Using the Context Switcher you can control which project you’re targeting with your code, which affects the intellisense you see while you’re coding. You can change your target emulator between various Phone 8.1 and Windows 8.1 options to debug each application in action.

All in all, this provides a pretty complete developer experience if you’re targeting the Windows platform – download the RC and give it a try!