Platform Specific Code In SAP And PCL

.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.

  1. Shared asset project ( SAP)
  2. 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.

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.

Compiler

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:

  1. using System;  
  2. namespace DevXamarinForm.Shared   
  3. {  
  4.     public class Common   
  5.     {  
  6.         public Common() {}  
  7.         public string PrintText()   
  8.         {  
  9.             string printtext = "No Device Specfic";  
  10.             #if __MOBILE__  
  11.             printtext = "iOS or Android specific code";  
  12.             #endif  
  13.             #if __IOS__  
  14.             printtext = "iOS specific code";  
  15.             #endif  
  16.             #if __TVOS__  
  17.             printtext = tv specific stuff ";  
  18.             #endif  
  19.             #if __ANDROID__  
  20.             printtext = "Android specific code";  
  21.             #endif  
  22.             #if _WINDOWS_PHONE  
  23.             printtext = "Android-specific code";  
  24.             #endif  
  25.             return printtext;  
  26.         }  
  27.     }  
  28. }  
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”.

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.
  1. using System;  
  2. using Xamarin.Forms;  
  3. namespace DevXamarinForm   
  4. {  
  5.     public class Common {  
  6.         public Common() {}  
  7.         public string PrintText() {  
  8.             string printtext = "No device Specfic Device";  
  9.             if (Device.OS == TargetPlatform.iOS) {  
  10.                 printtext = "iOS specific code";  
  11.             } else if (Device.OS == TargetPlatform.Android) {  
  12.                 printtext = "Android specific code";  
  13.             } else if (Device.OS == TargetPlatform.Windows) {  
  14.                 printtext = "Windows specific code";  
  15.             } else if (Device.OS == TargetPlatform.WinPhone) {  
  16.                 printtext = "Winphone specific code";  
  17.             } else if (Device.OS == TargetPlatform.Other) {  
  18.                 printtext = "Other specific code";  
  19.             }  
  20.             return printtext;  
  21.         }  
  22.     }  
  23. }  
Output

Output