I recently held a lab about the different techniques on how to share files in a Windows Universal app, thought I would share it . Usually files can be put in the shared folder and it works because like 90 % of the API between the phone and windows are the same. But what happens when they are not the same or you want a different behavior between the phone and windows? You still want to put all shared code in the shared project and then handle the different scenarios somehow.
I started with the notion that this lab was to be about universal app structure but quickly realized that the structure is that all code should be put in the shared folder except for when it is not possible. Therefore this lab was transformed into how to share files when it is needed. To find info on this subject was a little hard since universal apps are new and most of the info on the internet are simple Hello World examples which don´t get deep into the problems that can occur, usually all code in the examples are put in the shared folder and it just works. After a lot of search on the web I come across this book: Universal Windows Apps with XAML and C# Unleashed which sound perfect but will be released in February 2015. I also found this course from Microsoft http://www.microsoftvirtualacademy.com/liveevents/developing-universal-windows-apps-c-xaml-jump-start, it has been filmed but not released yet. Finally I found this session from TechEd New Zeeland http://channel9.msdn.com/Events/TechEd/NewZealand/2014/WIN305 this is a great session by Andy Wigley and most of the material in my session I have “borrowed” from his slides. You can find my slides from my presentation here.
In my session there are four techniques listed to use when you need to separate code from the shared file. They are:
- Partial classes
I put together a code example (found here at github) which to open a file, an image, from disk and show it in the UI. Almost all the code can be shared but Windows uses PickSingleFileAsync and the phone uses PickSingleFileAndContinue. This makes putting the file in shared impossible because it will not compile. By using the four different techniques I solved the problem, there are four branches in the repo which shows my solutions.
What are my findings then?
#if was the easiest of them all. In this simple example it is only one/two lines which differ so using #if to separate them is very quick. However when facing more complex code this way will be very messy and probably a little hard to follow the code.
Partial classes works pretty nice but there is little help from the studio and ReSharper. Even in this simple example I made a few typos which was found by the compiler with no prior warning. If one sometime in the future wants to change some function one is bound to forget to change both places.
Interface was very wrong for my example. Since the Windows code needs to call back to the view model using a interface became a little complicated. This could however be very useful under the right circumstances.
Inheritance did I found to be the nicest way to share code. By making the base class and function to be called abstract one forces the inherit classes in phone and Windows version to create correct new function. And in the future when changes are to be made all changes to the base class makes the inherit classes fail compilation if you forget to change them. There is help from the studio and ReSharper, when making typos there are squiggles. The code looks really nice and easy to find the correct functions your looking for.
So inheritance is my preferred technique when it comes to putting platform specific code in different projects. But there is always some scenarios where one of the other techniques will be better so keep and open mind to other solutions. Perhaps after I have watched the show from Microsoft virtual academy, mention above, when it is published my preference will change, I will update this post if that is the case.