In this walk through we will discuss the detailed structure and concepts of code sharing in Xamarin.Forms. Before going into this article it will be good and helpful if you go through the following article to get into the flow of Xamarin.
Only by having a deep knowledge in Xamarin will help you to maximize your ability to share code.
When we start to create a cross-platform project we will get a N+1 projects under a solution, where N represents the number of platforms you are targeting and the extra project will carry the code which will be shared across all your platforms.
Before going further you must be aware of the .NET frameworks used in Xamarin. The versions of the .NET Framework that are used in Xamarin.iOS, Xamarin.Android, and Windows Phone are not full versions of the .NET Framework. They are smaller subsets designed to work more efficiently on mobile platforms.
When we start to share code using Xamarin.Forms, we have universally accepted two methodologies of sharing:
- Portable Class Libraries
- Shared Projects
We can discuss more about those methods in the following sessions. According to your needs you can choose one of those methods in your project.
Portable Class Libraries
Portable Class Libraries or PCLs are nearly same as normal class libraries with limited functionalities. PCLs are designed with an idea of sharing code across multiple projects that target different versions of the .NET Framework. When you are working with a PCL, you have the ability to configure it to target the frameworks that are appropriate for your application. To get a full list of supported versions of the .NET Framework as well as functionality, just go through the documentation on MSDN about Cross-Platform Development with the PCL.
Let's start by creating a new project in Visual Studio or Xamarin Studio and select the Blank App (Xamarin.Forms Portable) template under Visual C# family. Give your new project a name, e.g. PortableDemo and click OK.
We will get our solutions created similar to the following screenshot:
Here the project name PortableDemo will carry all the shared code, and all the other projects, PortableDemo.Droid, PortableDemo.iOS and PortableDemo.WinPhone will carry the Android, iOS and Windows Phone platform codes respectively.
Advantages
- Non-platform specific code can be isolated in a separate project.
- The PCL project can be referenced easily by other projects in the solution.
- Shared codes in PCLs can be shared with other applications via a compiled.dll.
Disadvantages
- PCLs may not include any platform specific codes or classes available in MonoTouch of Mono for Android.
- PCLs can't be referenced due to the sharing of same class libraries between multiple platforms.
- PCLs only contain access to a subset of the .NET Framework.
Shared Projects
The Shared Project template was introduced with the release of Visual Studio 2013 Update 2, and Xamarin added support for these specialized projects in its release of Xamarin Studio 5.
Let's start by creating a new project in Visual Studio or Xamarin Studio and select Shared Project (Empty) template under Visual C# family. Give your new project a name, e.g. SharedProject1 and click OK.
If you didn't see a shared project template in your Visual Studio, just download and install Shared Project Reference Manager from Visual Studio Gallery.
By default, a Shared Project doesn't really contain access to any functionality at all. There is a strange thing going on here, just try to build, run or debug. It's crazy, right.? A Shared Project doesn’t actually build on its own. It's because, we build a project to transform our codes to an executable file which the system can use. Shared Project is merely a shell that contains code that will be included in other projects that reference it. There are no platforms to target or no rules to follow, this makes shared project entirely different from PCLs.
Now right click on the solution and add an Android Project to the solution. Secondly, right click on the Reference folder and select Add Shared Project Reference.
Now you can see the shared project SharedProject1, select it and click OK.
Now we can see that our shared project is added in our android project.
Now the code contained in shared project can be compiled into the referenced project. They need some other project to reference them for build and compile.
Shared project also supports some specific Compiler Directives. We can place special directives in our shared project so that specific platform can handle it differently. Some of the directives are listed here:
- __MOBILE__ : Defined in both Xamarin.iOS and Xamarin.Android projects.
- __IOS__ : Defined in Xamarin.iOS projects.
- __ANDROID__ : Defined in Xamarin.Android projects.
- WINDOWS_PHONE : Defined in Windows Phone 7 and 8 projects.
- SILVERLIGHT : Defined in Windows Phone Silverlight projects.
- __ANDROID_xx__ : Defined for specific API versions in android ( xx : replaced by targeted API version )
We can use context switchers such as #if, #else, and #endif directives which will help us point to the specific platforms in shared code.
Advantages
- Shared project can have platform specific codes.
- Support conditional compilation to allow multiple platform-specific code in a single file.
- It will allow access to libraries and all features of the parent project.
- Shared codes can be branched using context switchers based on compiler directives.
Disadvantages
- It will not support to create views using XAML.
- Unit testing became complicated since shared project contains many platform specific codes.
- Dll's are not created so codes can't be shared across other solutions.