
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 platformIf 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 NowNext up:
- Present a PowerPoint 2013 presentation online
- Poodle or terrier – are your systems safe?
- Ineffective goals
- Skype for Business – The missing “Lync”
- Planning and configuring Enterprise Content Management in SharePoint 2013
- Task types and scheduling in Project
- Display the message “Tasks due shortly” in Project
- Autofill to the Last Row Using VBA
- Staff Retention
- Using conditional formatting to highlight weekend dates in Excel
Previously
- Reuse slides in PowerPoint
- The System Prism
- SharePoint permissions on views using workflows
- Archiving old emails in Outlook
- Taking the first step towards power by rooting your mobile device
- For Each loops in Excel VBA
- How to give a killer presentation or…not let your presentation kill you!
- What? Cosmic rays?
- Quick ways to automate in Photoshop – Part 3: Batch processing
- New features of Microsoft CRM 2013