Shared Projects are the usual .NET application projects that contain code files and assets. Shared Projects are meant to be included in other projects and help in code reuse. It is a code level sharing technique.
A cross-platform application that supports iOS, Android, and Windows would require an application project for each platform and a separate Shared Project for the code common to all.
So, if you are creating a cross-platform app for Android, iOS, and Windows, you will usually have the following projects
- Shared Project – the Shared Project that contains the code which is common for all platforms viz iOS, UWP, Android
- AppAndroid – the Xamarin.Android project that specifically calls the underlying .NET APIs exposed for Android
- AppiOS – Xamarin.iOS application project that specifically calls the underlying .NET APIs exposed for iOS
- AppWindows – Windows application project that utilizes exposed Windows APIs and specific to UWP (Universal Windows Platform)
A Shared Project is not directly compiled. In other words, no DLL file is produced in the compilation process. Instead, the files are compiled into the same DLL as the project that references it. This way, it is possible to write blocks of platform-specific code in the Shared Project that will only be compiled by the specific platform.
The advantages of using this technique are:
- Allows you to share common code across multiple projects.
- The shared code can be branched based on the platform using compiler directives (eg. using #if __ANDROID__ or #if __IOS__)
- Application projects can include platform-specific references that the shared code can utilize
Disadvantages:
They do not produce any output assembly of their own. Hence, not used for sharing and distributing to other developers