This blog post is part of a series about how Windows Phone 8.1 affects developers. This blog post talks about how you can write for other platforms (i.e Android, iOS) and reuse that code in Windows Phone 8.1. This blog post is written by Peter Bryntesson (@petbry57)
Arguably the best way to have a single code base and target all major mobile platforms, is to write the in C#/.NET and use Xamarin to get this code to iOS and Android. The key to use this is to use Portable Class libraries, where you put common code that is shared between the different platforms. Here we see how the new application model in Windows Phone 8.1 affects this approach.
Business as usual
Writing portable code with Xamarin – A primer
To reiterate how you write portable apps across all mobile platforms using Xamarin. Take the following steps:
- Install Visual Studio and the Xamarin Tools.
- Structure your application according to the MVVM pattern.
- Put your Models and ViewModels in a Portable Class Library(PCL) that we can reuse across platforms.
- Implement device specific functionality for each platform and dependency-inject those into the PCL.
- Implement your views natively for each platform using Visual Studio for Windows and Windows Phone, using Xamarin for iOS and Android.
Very briefly, that’s the steps we need to take. What approach could we take for targeting Windows Phone 8.1 / Windows 8.1? I would recommend doing a Universal app that reuses the the PCL, and to put as much code as possible into the Shared folder between these two platforms.
Developing with Xamarin remains the best option when you need to target all major mobile platforms with a single codebase. Windows Phone 8.1 makes this even easier, since the Windows and Windows Phone versions now can share even more code.