.NET Class and API or some features behave differently on each platform. Thus, we need to write some code on the specific platform. Below are the two libraies for sharing the code between the multiple platforms.
- Shared asset project ( SAP)
- Portable Class library (PCL)
Share Project (SAP)
You can share the code across the multiple platform projects. Your code is compiled as a part of each referencing project and can include the compiler directives to help incorporate platform-specific functionality into the shared code base.
Xamarin project template has a standard for defining the Build Symbol with the double underscore pre and post – fix.
Build Symbol |
Description |
__MOBILE__ |
Supports iOS and Android specific code. |
__IOS__ |
iOS specific code. |
__TVOS__. |
TV Specific code. |
__WATCHOS__ |
Watch Specific code. |
__ANDROID__ |
Android Specific code. |
__MAC__ |
Mac Specify code. |
_WINDOWS_PHONE and SILVERLIGHT – |
Windows Phone Specific code. |
Visual Studio
The compiler directives are on your platform specific project. Right click on your Platform Specific Project - click on property - Select Build Option.
Change the Configuration drop downs at the top of the options to see the symbols for each different build configuration.
Xamarin Studio
Right-click Project > Options > Build > Compiler > Define Symbols.
Change the configuration drop down at the top of the options to see the symbols for each different build configuration.
Define the symbol, given below in the shared project and Add reference into all the platform projects. Assign the value into Text box control or other controls:
- using System;
- namespace DevXamarinForm.Shared
- {
- public class Common
- {
- public Common() {}
- public string PrintText()
- {
- string printtext = "No Device Specfic";
- #if __MOBILE__
- printtext = "iOS or Android specific code";
- #endif
- #if __IOS__
- printtext = "iOS specific code";
- #endif
- #if __TVOS__
- printtext = tv specific stuff ";
- #endif
- #if __ANDROID__
- printtext = "Android specific code";
- #endif
- #if _WINDOWS_PHONE
- printtext = "Android-specific code";
- #endif
- return printtext;
- }
- }
- }
Portable Class library: [compiler directives do not work in PCLs]
Portable class libraries are platform independent. PCL do not allow us to use the conditional compilation. This is because PCL should work on all the specified platforms, which were chosen as a target. Also, the availability of features depends on the selected targets.
Let us see, how we will do the same, mentioned above.
Step 1: Create class file like above under PCL project
Step 2: Right click Specific Platform project - Add - Existing Item -Select PCL project - Select Common Class file - select “Add as Link”.
It will work the same as Shared Project.
Recommended approach to writing platform conditional code in a PCL
The device class contains a number of properties and methods to help the developers customize the layout and functionality on a per-platform basis.
- using System;
- using Xamarin.Forms;
- namespace DevXamarinForm
- {
- public class Common {
- public Common() {}
- public string PrintText() {
- string printtext = "No device Specfic Device";
- if (Device.OS == TargetPlatform.iOS) {
- printtext = "iOS specific code";
- } else if (Device.OS == TargetPlatform.Android) {
- printtext = "Android specific code";
- } else if (Device.OS == TargetPlatform.Windows) {
- printtext = "Windows specific code";
- } else if (Device.OS == TargetPlatform.WinPhone) {
- printtext = "Winphone specific code";
- } else if (Device.OS == TargetPlatform.Other) {
- printtext = "Other specific code";
- }
- return printtext;
- }
- }
- }
Output