Search

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, August 4, 2016

Xamarin.Forms: Platform specific code in SAP and PCL

Introduction

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





Tuesday, August 2, 2016

Q A - Xamarin.Form: Don’t want to target one or more platform at a same time?

Create New Xamarin.Form solution after that clear unwanted target project. If you required to add project on feature.


You can add new platform projects to the Xamarin.Forms solution.

 In the Add New Project dialog, you can create Xamarin.iOS project by selecting the iOS project Universal type and Blank App template. 

Create a Xamarin.Android project with the Android Blank App template, or a Windows project by selecting Universal under the Windows heading (for a UWP project), or Windows or Windows Phone under the Windows 8 heading, and then Blank App.