Xamarin.Forms: Platform specific code in SAP and PCL

.Net Class and API or some features that behave differently on each platform so we need to write some code on platform specific. Below two library for sharing code between multiple platform
  • Shared asset project ( SAP)
  • Portable Class library (PCL)
Share Project (SAP)

You can share code across multiple platform projects. Your 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.

Xamarin project template have standard for defining Build Symbol with double underscore pre and post – fix

 
 Build Symbol
  Description
__MOBILE__
Support 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 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 downs at the top of the Options to see the symbols for each different build configuration



Define symbol like below in shared project and Add reference into all the platform project and assign value into textbox control or others control

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 to use conditional compilation .This is because PCL should work on all specified platforms which was chosen as a target and Also, availability of features depends on selected targets.

We can use same above way but that is not recommended way. Let we see how we will do same 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 same like Shared Project.

Recommended approach to writing platform conditional code in a PCL:

The Device class contains a number of properties and methods to help developers customize 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:

0 Comments

Featured Post

Improving C# Performance by Using AsSpan and Avoiding Substring

During development and everyday use, Substring is often the go-to choice for string manipulation. However, there are cases where Substring c...

MSDEVBUILD - English Channel

MSDEVBUILD - Tamil Channel

Popular Posts