Xamarin: Portable Class Library and Shared Project

X

Xamarin is well known for its code sharing approach, basically there are two ways/methods for sharing code between Xamarin apps, Portable Class Library (PCL) and Shared Asset Project (SAP). Both have their own benefits but PCL is quite popular because of its nature. We will be discussing  how PCL and SAP works through the rest of the article.

New Project Templates For Xamarin Cross Platform Apps in Visual Studio

Shared Projects

In a Shared Asset Project each project will be compiled against each and every platform (Android, iOS, Windows Phone etc). Platform specific compiler directives such as #if #ifdef etc can also be used in shared projects. The complexity of a shared project increases because you also write the platform specific code inside it which can mess up your code in future.

A shared project can be really useful for a small project/solution but going forward if you are targeting for the app maintainability then it might be a really bad solution, shared project becomes less than pleasant due to lack of architectural control and struggling to find what is actually running on each platform.

You will also faces issues when defining an assembly name. For example in XAML if you wanted to reference a control and reference an assembly name, each native project will have a different assembly name, so you either have to do some more #ifdefs or On Platforms, or have to name each native project the same assembly name.

Portable Class Library

Portable Class Library (PCL) are compiled against a general .NET subset which is compatible to all platforms you want.

PCLs provide a great way to write code once that can be used on multiple platforms, easily maintained and tested. When you write a PCL you don’t need to insert compiler directives to switch code depending on each platform, you select which platforms you will support then starting coding.

So what happens when i want to access the platform specific code inside a PCL, you ask ?

You do this by writing the platform specific code within the native project (or a native class library). In a common PCL, create an Interface that both your native platform projects depend on. Use dependency injection to inject that class into your project and now you have access to the platform specific code from any PCL referenced by both projects.

well for example If you want to access the camera inside the PCL code then you need to access it by a generalized interface via dependency injection. There are some pretty good frameworks helping you to achieve this goal. One of the most famous is MVVMCross.

Xamarin also has a great tutorial to get started with the dependency service. Click on the below link to go through it.

https://developer.xamarin.com/guides/xamarin-forms/dependency-service/

Target and Profile Selection

For Xamarin Forms projects, it is best to use this common setup. It allows you to target all 3 platforms.

PCL’s are projects that can target different platforms, you can choose which ones and then it gives you access to certain assemblies and functions depending on which ones you select. The different combinations targets for PCL’s are referred to as Profiles and each Profile has its own number. The most common profile is  Profile259.

Benefits of using PCL

  • Easier unit testing
  • Readable code
  • Portability
  • Separation

Portable Class Library vs Shared Projects

PCL SHARED PROJECTS
Comparision of PCL and Shared Projects
Have access to a subset of .Net framework Full Access to .Net Framework
cannot use #ifdef directive because it is an independed library. can use #ifdef directive for platform-specific code
Platform-specific code requires Inversion of Control (IoC) Platform-specific code doesn’t requires Inversion of Control (IoC)

 

Which One to Use ?

With C# it is now possible to implement MVVM either as aPortable Class Library or Shared Project. But when you’re using XAML you must write aPortable Class Library. So that makes you choose PCL when you are targeting for all the three platform iOS, Android and Windows.

This might be a disadvantage if you want to use all the compiler directives inside your project because PCL won’t let you use them. But in Xamarin PCL you can achieve similar kind of outcome using the Device class.

Though Device class although part of the Portable Class Library, it has methods that return different values depending on the native platform your App is installed on.

Other useful Device functions:

  • Device.OS returns an enum so you know what Operating system you are on. enum TargetPlatform {Other, iOS, Android, WinPhone, Windows}
  • Device.OnPlatform this allows you to choose an appropriate value dependent on platform. This function can be used directly in XAML coding whereas most Device functions will require you to interface to CSharp to access.
  • Device.Idiom another enum that will tell you if you are on a Tablet or a Phone.
  • Device.GetNamedSize used for getting fonts using device relative sizing.

References

 

About the author

Subodh Jena

I'm an open minded liberal type with curiosity as perhaps my main motivator. I love science, art, the natural world and learning from people of diverse backgrounds.

Add comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

By Subodh Jena
%d bloggers like this: