Hello World! Build your first Xamarin.Forms

Xamarin.Forms
img_xamarin

Xamarin.Forms is a cross-platform natively backed UI toolkit abstraction that allows developers to easily create user interfaces that can be shared across Android, iOS, and Windows Phone. The user interfaces are rendered using the native controls of the target platform, allowing Xamarin.Forms applications to retain the appropriate look and feel for each platform.

Xamarin.Forms is implemented as a .NET Portable Class Library ( PCL), which makes it very easy to share the Xamarin.Forms API’s across a variety of platforms. The first step to getting started is to create a solution for the various projects that will make up the application.

A Xamarin.Forms solution can be created in Xamarin Studio or Visual Studio and will typically contain the following projects:

  • Portable Library – This project is the cross platform application library that holds all of the shared code and share UI.
  • Xamarin.Android Application – This project holds Android specific code and is the entry point for Android applications.
  • Xamarin.iOS Application – This project holds iOS specific code and is the entry point for iOS applications.
  • Windows Phone Application – This project holds the Windows Phone specific code and is the entry point for Windows Phone applications.

In this tutorial we are just going to see how to create Xamarin.Forms and try to understand its projects and working of important files.

Let’s Start:
To create a Xamarin.Forms application we are going to create a new solution, go into Xamarin and New solution

c

Go into C#->Mobile Apps-> Blank XamarinApp(Xamarin.Forms Portable) and give name of solution as “HelloXamarinForm”

xf1.2

In Solution Tab you can see files for your iOS and Android and also a common project , for windows phone development you have to use Visual Studio with Xamarin.

xf1.3

Now go in HelloXamarinForm, you will find HelloXamarinForm.cs. Open the file:

Here, App is the main class that is responsible to generate UI for Android and iOS.
-MainPage gets instance of ContentPage which is a type of Page in Xamarin.Xorms that is used to display content.
-StackLayout is a type of Linear layout that holds controls in it.
-Here we simply declare a Label to display text in our view, we can create a instance of label and use it in our view but that we are going to do latter in our tutorial.

We have a Android and a iOS project in our this solution. This App class is going to be used in that both plateform to load UI and backend code.

Go to HelloXamarinForm.Droid and open MainActivity.cs:

Here in LoadApplication method we create an instance of App that will initialize Xamarin.Forms framework, and then load your Xamarin.Forms application.

Now go to HelloXamarinForm.iOS project and open AppDelegate file:

Here in AppDelegate file as same as Android we create a instance of App and initialize the Xamarin.Forms framework and start up the Xamarin.Forms application. This is done inside the FinishedLaunching method, as demonstrated in the code.

Try to build and run the app and you will see the application is run in both Android and iOS.

xf1.4

xf1.5

If you like this tutorial then you can download full copy of the code from github

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">