Quick Start Tutorial: Creating Universal Apps Via Xamarin - Part One

Introduction

Xamarin SDK is one of the most popular development tools to create cross platform mobile applications for iOS, Android, and Windows Phone. Recently Microsoft has acquired Xamarin (February 24, 2016) and it's free (from March 31, 2016 onwards) to all developers to create mobile apps like Visual Community edition.

To develop the Xamarin application two options are available, Xamarin IDE and Visual Studio. Microsoft has highly recommended using the Visual studio IDE and Microsoft has stopped developing the Xamarin IDE for Windows OS. The Xamarin package is completely included in the Visual studio 2015 update 2, and  no separate download is required.

Universal Windows Program

Xamarin supports Windows phone 8.1 and Windows 10 Mobile. Hey wait -- Windows 10 mobile is developing via universal windows program: Did Xamarin support UWP program? Will my application run in the Windows 10 Desktop? YES 100 percent, the Xamarin application is running in the Windows 10 Desktop; also, a cool feature is one code base runs in both the Mobile and desktop.

Xamarin is using a single language C# to write the Native-based code application for iOS and Android or Cross platform applications, it's better and is divided into three types  of the application Xamarin supports,

  1. Native approach
  2. Traditional approach
  3. Forms approach


Write the application based on the Native based API -- the application is only targeting a specific platform. The traditional approach shares only the business logic, each platform has a different UI code.

Forms approach: Forms approach is supporting both UI and business logic code (i.e.) write once run any platform

Xamarin Forms

Xamarin forms are used to share the complete UI logic (Design the form) to all the platforms. Xamarin Forms is divided into two types,
  1. Xamarin. Forms. Portable
  2. Xamarin. Forms. Shared

Portable type each platform is referenced to Portable DLL; there is no platform-specific API code to support this option.

Shared type option: shares the code for each platform specifically (Portable type ref reference(Dll)), write the code based on the conditional compilation option and it supports platform-specific API.

Conditional Compilation: Conditional compilation is used to define the condition to compiler which code should be executed, pre-processor directive is used to define the conditional compilation.

#if
#else
#elif
#endif


Example

  1. #if _UWP  
  2.  
  3. #elif _WindowsPhone  
  4.  
  5. #elif _Android  
  6.  
  7. #elif _iOS  
Platform specific, writes the code inside the #if or #elif block, based on the compiler the corresponding code will execute, and where is the option to define the condition to the compiler, there are two ways we can define the option,
  1. Use the #define option
  2. Declare the option in the properties page

#define: This option declares the condition variable in the code file like below
#define UWP
#define WindowsPho
ne

Properties page

Define this option in each project properties page (e.g.) Android-> Project Properties -> Build -> Conditional compilation symbols -> UWP.



Best practice uses defined directly in the Properties page.

Note

This is the part of the series in which  we are going to use the Xamarin Forms. It's portable, 100 percent of writing the code is based on Xamarin based SDK and controls, there is no platform specific code.

The advantage of this option is you write it once  and it runs on  any platform, Xamarin supports 40 common controls to support all the platforms and run time. These  controls are rendered to the native controls (i.e.) ex: In Button control each platform, GUI represents these  this controls in different styles.

Disadvantage of this option is only limited controls are available, we cannot use the platform specific controls and API also.

Requirements

  1. Visual studio 2015 Enterprise or Community Edition Update 2 (Highly recommend to use Visual studio IDE in Windows OS Platform)
  2. Windows 10 OS (Run the Universal Windows Platform application)
  3. Windows 10 SDK & Android SDK
  4. Xamarin Android player Simulator (Compare to other simulator, this simulator is fast)


Similar Articles