New Fresh deep-dives on .NET, Azure & AI now publish on blog.msdevbuild.com Read the latest →
Xamarin

Xamarin.Forms: Platform specific code in SAP and PCL

By   Friday, January 31, 2020 comments
reads
React:
.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:

SJ
Suthahar Jegatheesan

Senior Solutions Architect · Microsoft Azure, AI & Mobile. Author of the Xamarin Q&A Book.

More in this series

No comments:

Practical Learn .NET, Azure & AI

sponsored

What Colleagues Say

“Suthahar is one of the most thorough and knowledgeable cloud architects I’ve worked with. His depth on Azure and ability to make complex mobile solutions work cross-platform is remarkable. He always delivers beyond expectations.”

AT Arun T. Senior Engineering Manager · Direct Manager

“I’ve followed Suthahar’s MSDEVBUILD blog for years — practical, deep, always current. Working alongside him confirmed what I suspected: he’s as sharp in person as on paper, and a genuinely generous mentor and architect.”

RK Rajesh K. Lead Developer · Mindtree

Certifications & Recognition

AZ-300 · Azure Architect Technologies AZ-301 · Azure Architect Design AZ-104 · Azure Administrator Associate AI-100 · Designing Azure AI Solutions DP-900 · Azure Data Fundamentals MCPD · Windows Phone Certified SAFe® 4 Scrum Master

Technical Expertise

☁ Cloud Platforms Microsoft Azure — Expert · 18 yrs App Services · AKS · Functions · Cosmos DB · APIM · Entra ID AWS & Google Cloud — Advanced
πŸ€– AI & Automation Azure OpenAI / GenAI — Advanced · 5 yrs RAG Pipelines · Bot Framework · Cognitive Services · Vision & Speech AI · ML.NET
πŸ“± Mobile Architecture .NET MAUI & Xamarin — Expert · 12 yrs Flutter · Native iOS/Android · Offline-First · Secure Storage & Auth
πŸ”’ Identity & Security Microsoft Entra ID — Expert OAuth 2.0 · OIDC · Zero Trust · Conditional Access · RBAC · Key Vault

Speaking & Community

Microsoft User Group Malaysia Recurring speaker on Azure architecture, AI integration and .NET mobile development across Malaysia and APAC.
Global Azure Bootcamp — Chennai Presented “AI into Xamarin Mobile Apps” at GAB Chennai, part of the worldwide Global Azure community event.
AI & Azure OpenAI Community Events Workshops and live demos on RAG pipelines, AI agents and intelligent workflow automation since 2023.
Mobile & Cloud Workshops — Coimbatore Multi-day deep-dive sessions at GRG Computer Technology, KG Microsoft Innovation Center and RVS College of Arts and Science.
500+Blog Articles
50+Conference Talks
10K+Monthly Readers
30Public Repositories
3Continents Delivered
MSDEVBUILD Assistant Ask about Suthahar, Azure, AI or mentorship
Hi! I’m a simple FAQ bot for MSDEVBUILD. Tap a question below or type your own.