Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Shared Library and PCL

Tags:

What is the exact difference between xamarin shared project and portable class library?

When to use shared library and when to use portable class library?

Is this possible to write native functionality in shared projects like showing alert,accessing camera and use it for both android and iOS?

Can anyone please explain me.

like image 578
PC Parker Avatar asked Aug 17 '15 06:08

PC Parker


People also ask

What is difference between PCL and shared project in Xamarin?

Shared Projects work well for application developers planning to use lots of platform-specific functionality in their cross-platform apps. While PCL projects continue to be supported in Visual Studio, . NET Standard is recommended for new projects.

What is PCL in Xamarin forms?

What is a Portable Class Library? When you create an Application Project or a Library Project, the resulting DLL is restricted to working on the specific platform it is created for. This prevents you from writing an assembly for a Windows app, and then re-using it on Xamarin. iOS and Xamarin. Android.

What is the difference between .NET Standard and PCL portable class libraries?

NET Standard is platform-agnostic, it can run anywhere, on Windows, Mac, Linux and so on. PCLs can also run cross-platform, but they have a more limited reach. PCLs can only target a limited set of platforms.

What is shared project in Xamarin forms?

Shared Projects let you write common code that is referenced by a number of different application projects. The code is compiled as part of each referencing project and can include compiler directives to help incorporate platform-specific functionality into the shared code base.


1 Answers

  • In shared projects each code file will be compiled for each destination (Android, iOS, Windows Phone etc). You are able to include platform specific code by using #if compiler directives. When you want to access the camera you need to write the access code inside an #if block for all destinated platforms. This can mess up your code but it can be easier to find the different implementations. Learn more: http://developer.xamarin.com/guides/cross-platform/application_fundamentals/shared_projects/
  • Protable Class Libraries (PCL) are compiled against a general .NET subset which is compatible to all platforms you want. So you can access System.Net.Http but you cannot access any platform specific code. 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 archieve this goal. One of the most famous is MVVMCross (https://github.com/MvvmCross/MvvmCross/wiki). Learn more about PCL: http://developer.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/sharing_code_options/#Portable_Class_Libraries

I personally perefer PCLs because the code is much easier to read without any compiler directives. Using MVVMCross you are able to use plenty of plugins via NuGet. So you don't need to write your own classes for camera access, showing alerts etc.

like image 68
Wosi Avatar answered Oct 21 '22 21:10

Wosi