Building mobile app with Xamarin.Forms


Resource:  creating-mobile-apps-with-xamarin-forms

Creating a platform-independent application with Xamarin.

I.  Introduction :

History:  After announcement of .Net in 2000, the company ximian initiated a open source project called Mono to run the C# compiler and .Net framework on Linux. and founded Xamarin in 2011.

1. When a programmer develops an application that targets multiple mobile platforms using the MVVM architecture, it helps guide the developer into separating code into the 

platform-specific Viewthe code that requires interacting with the platform API 
platform-independent Model and View- Model.

2. The part of the application that is platform independent can then be isolated andin the context of Visual Studio or Xamarin Studioput into a separate project. This can be either a 

Shared Asset Project (SAP)which simply consists of code and other asset files accessible from other projects or
Portable Class Library (PCL), which encloses all the common code in a dynamic-link library (DLL) that can then be referenced from other projects.

3. A Xamarin.Forms application in Visual Studio consists of five separate projects for each of these five platforms, with a sixth project containing common code. But the five platform projects in a Xamarin.Forms application are typically quite smalloften consisting of just stubs with a little boilerplate startup code. The PCL or SAP contains the bulk of the application, including the user- interface code.




The Xamarin.Forms.Core and Xamarin.Forms.Xaml libraries implement the Xamarin.Forms API. 

Depending on the platform, Xamarin.Forms.Core then makes use of one of the Xamarin.Forms.Plat- form libraries. 

These libraries are mostly a collection of classes called renderers that transform the Xamarin.Forms user-interface objects into the platform-specific user interface.


For example, suppose you need the user-interface object discussed earlier that allows the user to toggle a Boolean value. When programming for Xamarin.Forms, this is called a Switch, and a class named Switch is implemented in the Xamarin.Forms.Core library. In the individual renderers for the three platforms, this Switch is mapped to a UISwitch on the iPhone, a Switch on Android, and a ToggleSwitch on Windows Phone.

4. In Xamarin.Forms, the objects that appear on the screen are collectively called visual elements.    They come in three main categories:
  • page
  • layout
  • view

    These are not abstract concepts! The Xamarin.Forms application programming interface (API) de- fines classes named VisualElementPageLayout, and View. These classes and their descendants form the backbone of the Xamarin.Forms user interface. VisualElement is an exceptionally important class in Xamarin.Forms. A VisualElement object is anything that occupies space on the screen.

II. Creating Xamarin app in Visual Studio for Mac:

1. Click on File -> New Project -> Choose MultiPlatform -> App ->Blank Forms App (Choose C#)  


2. Give the app a name ("demo app") and choose organization and the folder

3. The Visual studio should create three projects.
   -demoapp (PCL project)
   -demoapp.Android (For Android)
   -demoapp.ios (For Apple)

As of Now, Visual studio cannot create UWP for windows 8/8.1 application

4.  The solution should look something like this
Lets look at App.Xaml.cs
It has a constructor that InitalizesComponent and creates a MainPage instance.

The MainPage.Xaml has the following text 


iOS devices

When you build and deploy your applications it will look like this


If you look at the demoapp.ios project, it contains AppDelegate.cs which has the following code


FinishedLaunching() is what does the magic of using the actual code in XAML app and uses the boiler code to do the mapping.

Main.cs in the iOS project:


Android:


In the demoapp.Android project, it starts from MainActivity.cs 


It maps a label to the demoapp and the rest of the code is to handle if the device is used landscape.


This marks the end of tutorial on how you can start building your mobile app using Xamarin.






Comments