This now has little relevance. A lot has changed, was created the . NET Standard, other ways to organize things in Xamarin, and in 2020 the . NET 5 that changes even more.
I don’t work with Xamarin (yet) so I can’t talk with experience, and to not have to trust what a nerd like me, I went researching on the subject.
I found a article by Miguel de Icaza, the creator of Xamarin. He recommends using Shared in contrast to Jason Smith who prefers PCL.
I found it interesting because it validated something that I always thought. With partial
we don’t always need to use #ifdef
.
It sounds like opinion, and it even is. In fact, if there are both ways, they meet different situations and different tastes. I stay with Miguel, not because he’s the big deal, but because it makes more sense to me. Let’s see
PCL
- Same code can be used for multiple platforms
- All maintenance affects all platforms in a unique way, no need to replicate
- Executable can be shared between different projects
- Allows the application to be pluggable dynamically
But
- Only one subset of the . NET is available
- The application can only use the most basic parts that can work evenly across supported platforms.
- You won’t always have the best user experience
- The code tends to become complex to try to make it work well on all platforms
Shared
- It’s easy to share code with multiple projects and some parts of code can work across platforms
- You can use compiler directives or
partial
to compile the parts of each platform
- The code tends to be simpler
- The application works as expected on that platform and uses everything available on it
But
- Has to generate a monolithic executable and can not share with other applications
- Most of the development, but not all, should be done for each platform, which generates a certain duplication
- Maintenance has to be done on every code of every platform
- The application is what it is, if you want to improve it with something extra, you don’t have the option to do this dynamically, you have to generate another application
It’s obvious that if you don’t need more than one platform makes it easy to choose since you don’t need to share anything.
PCL can be more useful if you need to make applications for multiple platforms quickly and cheaply, even if the result isn’t as good. It is also interesting if it will be common for the user to have several different applications with the same base running on their device.
Shared is more interesting when you want the best possible result on each platform and in general the application will be unique on that basis.
More can be read on Xamarin.Forms Portable or Xamarin.Forms Shared