Porting existing .NET apps to Four Mobile Platforms with PCL
One of the big wins for C# developers with the announcement of collaboration between Microsoft and Xamarin is that Portable Class Library (PCL) projects are now fully supported on both iOS and Android, making it easier than ever for C# developers to share code across devices. What this also means is that your existing .NET apps can be easily ported to mobile by moving the code that is platform agnostic to a portable class library that can be referenced from iOS, Android, Windows Phone, and Windows Store projects. Almost any .NET codebase, including Windows Forms, WPF, ASP.NET, and Silverlight, has sharable code that can be ported to Xamarin.iOS, Xamarin.Android, Windows Phone & Windows Store.
Previously, we looked at a step-by-step approach to port an existing Silverlight app to iOS and Android in Visual Studio. In case you missed it earlier, here are the links to it:
In practice, you can use these techniques to port any .NET code to mobile, but for this example I thought a Silverlight MVC sample had the potential as a mobile app. The Diet Calculator example uses a simple algorithm to calculate the daily calories required based on inputs provided by the user.
Here’s how the ported Silverlight app looks on iOS, Android, and Windows Phone devices:
In this blog post, I will quickly recap the steps that were involved to separate reusable code into a core library and then move it to PCL. In case you are in a hurry to look at the code, you can download the DietCalculator code for Xamarin.iOS, Xamarin.Android, Windows Phone, Windows Store and Silverlight using PCL from GitHub.
Scan Code for Mobilization
We used Xamarin’s .NET Mobility Scanner to analyze how mobile our Silverlight code is and concluded that 78% of the app code can be re-used in all platforms. As per the detailed report, all of the platform-specific method calls (i.e., code that we can’t re-use across platforms) are user-interface specific and hence the business logic code that consists of Models, Controllers and Helpers from the Silverlight_MVC project can be re-used in all the platforms.
As per the report, 100% of the code can be ported to Windows Phone. However you definitely do not want to re-use the Silverlight UI on Windows Phone. Just like how we built UI natively for iOS and Android, you need to follow the Windows Phone design guidelines to build an amazing user interface that is truly native to the platform.
Separate Reusable Code into a Core Library
When we set up our solution, we followed the separation of responsibility principle and moved our Models, Controllers and Helpers from the Silverlight application to a core project called DietCalculator.Core and referred them using File Linking on iOS and Silverlight projects. This time, instead of using File Linking, let’s make this project a Portable Class Library and reference that project from Xamarin.iOS, Xamarin.Android, Windows Phone, Windows Store. For an overview of how to architect a cross-platform application to maximize code re-use, be sure to check-out our detailed documentation on Building Cross Platform Applications. Also, do check out our documentation on Introduction to Portable Class Libraries for creating and consuming PCL projects in Xamarin Studio and Visual Studio.
Creating a Portable Class Library in Visual Studio
In Visual Studio, you can create new Portable Class Library projects and reference them from within your Xamarin.iOS, Xamarin.Android, Windows Phone, and Windows Store projects,as well as other projects such as WPF and Silverlight. That means that you can write rich cross-platform libraries and take advantage of them from all of your .NET apps.
To create a new portable class library to your solution in Visual Studio:
Right Click on Solution Explorer > Add > New Project > Visual C#> Portable Class Library (DietCalculator.Core)
Make sure to select Xamarin.Android and Xamarin.iOS targets (shown below)
In your PCL project, create a folder called “Core”, move the Models, Controllers, and Helper files to it and rename their namespaces for clarity. Now reference this project within your Xamarin.iOS, Xamarin.Android, Windows Phone, Windows Store and Silverlight projects. That’s it!
You can also live debug inside portable class libraries. Here’s an example of debugging an iOS project referenced PCL code:
Note: Video is soundless.
Please note, the logic in the program is ported as is — so all calculation results will depend on the algorithm used by the original author 🙂 You can download the sample code from github.