Code sharing strategies in Windows Universal Apps

 Jun 26, 2015

I have previously introduced you to the amazing concept of Universal Apps, which essentially means that you can share most of the source code between Windows Store Apps and Windows Phone Apps. In today's article, I will go beyond the basics and show some additional code sharing strategies.

When you start a new project in Visual Studio 2013 and you pick one of the Universal Apps templates, you will end up with 3 projects in your solution: one for Windows, one for Windows Phone and a Shared project. Any classes you add to the shared project will be automatically compiled into the 2 platform specific projects. If there is code that is specific to one of the platforms, we have seen in our previous blog that the standard way to cope with the differences is by using conditional compilation code directives as you can see in the following code snippet.

#if WINDOWS_APP //code that compiles only for Windows platform #else //code that compiles only for Windows Phone platform
If we start using this strategy too often, the code will become increasingly difficult to read and to maintain. Let's say we add a class called Base into the Shared Project. We know that the 2 platform projects will be able to call all public methods in that Base class as the class is simply compiled independently into both projects.
public class Base { public string Test1(){ return 'Shared'; } public virtual string Test2(){ return 'Shared'; }

Note that the method 'Test2' is declared as virtual. So, if we have a class that inherits form Base we can override Test2.

Now instead of using the same class Base in both platform specific projects and add a lot of conditional compilation code directives to implement methods in different ways for each platform, we can add to both projects a class that derives from Base. Let's say we will call this class 'Derived' and we override the virtual method 'Test2' as we see in the following code snippet for the Windows Phone platform.

public class Derived : Base { public override string Test2(){ return 'Windows Phone'; }
We do the same thing for the Windows platform, but change the implementation of the virtual method.
public class Derived : Base { public override string Test2(){ return 'Windows'; }

Visual Studio requires that if a class is added to one platform project, it must also be added to the other one. So even if we only wanted to override some methods from Base in the Windows Phone project, we must have a class with the same name in the Windows project as well. But note that the fact that a method is declared as virtual does not mean that it must be overridden. So we could have a class with an empty body and use the implementation that was provided in the Base also for virtual methods as we see in the next code snippet.

class Derived : Base { }

This technique is an alternative to the overuse of conditional compilation. The basic idea is to add a class to the Shared project that declares methods that will have a different implementation in one of the platforms as virtual. You could provide code in the body of the virtual method that runs fine in one of the platforms and then override the method for the other platform.

How do your Excel skills stack up?   

Test Now  

About the Author:

Newton Godoy  

With over 17 years of in-class training experience and over 16 years of industry experience, Newton offers students a wealth of real-world technical knowledge and expertise in the areas of .NET application development, SQL Server and SharePoint Server. After spending several years lecturing as a professor, Newton found his true calling and began his career as a MCT. He worked as a technical trainer for some of Brazil’s and Australia’s largest corporate training organisations before finally finding a home with New Horizons where he is now one of our top trainers. Newton brings a thorough mentoring capability to the classroom where he can advise on technical issues and challenges often beyond the scope of the course curriculum. His combination of technical knowledge and instructor experience make him one of the most respected instructors within the IT training industry.

Read full bio