Home > C#, Visual Studio, Windows Phone, WinRT > How to share code among different Universal Windows apps

How to share code among different Universal Windows apps

22/04/2014

By this time, every Windows developer knows about Universal Windows apps, that enable building applications on the converged Windows 8.1 and Windows Phone 8.1 platforms.

A key concept of this new approach is the Shared Project, a particular kind of project that doesn’t have any binary output, but simply allows sharing source between converged apps.

The new Univeral app projects

The new Univeral app projects

Of course, we have the possibility to tailor the design to each device. We can also use conditional if and partial classes in the Shared Project to define platform specific code, that will be compiled within each app. In this way, we have a single common point between Windows 8.1 and Windows Phone 8.1 projects, but we can also write custom code for each app.

But what if we want to share code among different Universal Windows apps, because for example we want to redistribute our libraries? First of all, we can think about using Portable Class Libraries. This type of project has been improved in Visual Studio 2013 Update 2 and now, if we target Windows 8.1 and Windows Phone 8.1, we’ll be able to use all the common APIs. However, PCL produces a single binary that works as is in all the supported projects. Handling divergent APIs requires using higher-level abstractions, such as dependency injection or IoC containers. We can’t simply use conditional if to define platform specific code.

Luckily, there is another solution. Some days ago has been released a Visual Studio 2013 extension called Shared Project Reference Manager that allows to use the same concept of Universal app Shared Project with almost any C#, VB.NET, C++ or WWA/JavaScript projects. Let’s see how to use it.

First of all, create two Class Libraries, one for Windows and one for Windows Phone. They will contain any platform specific code, if necessary.

Libraries for Universal apps

Libraries for Universal apps

Then, we need to add a new Shared Project to the solution. This template has been added to the IDE by the Visual Studio extension mentioned before.

Shared Project

Shared Project

After that, right click on the References node on both the class libraries, select the Add Shared Project Reference command and, in the new window, click on the shared project we just created.

Shared Project Reference Manager

Shared Project Reference Manager

Now we can start creating any file in this new Shared project, as we do in the standard Shared Project created by the Universal app project template: everything we add to it will be automatically available in both the platform specific libraries.

Shared Project Library

Shared Project Library

In this way, we can develop and distribute libraries that allow code reuse, and thanks to the Shared Project all the common portion between Windows 8.1 and Windows Phone 8.1 can be written in a single place, with the ability to use conditional if to define custom behavior. Let’s see a simple example:

// In the project "UniversalLibrary.Shared
public class Utility
{
    public string GetGreetings()
    {
        var greetings = "Hello from {0}!";

#if WINDOWS_APP
        greetings = string.Format(greetings, "Windows");
#endif

#if WINDOWS_PHONE_APP
        greetings = string.Format(greetings, "Windows Phone");
#endif

        return greetings;
    }
}

When compiled, the UniversalLibrary.Windows library will contain the code at line 9, while UniversalLibrary.WindowsPhone will contain the one at line 13. So, starting from a single code file, we have easily realized two class libraries.

Note that, in most cases, the Windows and Windows Phone projects remain empty, as they are needed only to generate the output libraries that can be added to our Universal apps or distributed.

Comments are closed.
%d bloggers like this: