How to use XAML in Xamarin.Forms

Xamarin.Forms
img_xamarin

Xamarin.Forms provides 2 type of Pages for creating Layout for multiple platforms
1) With Code in C# file
2) With XAML file

Until now we have seen how to add different components and controls in C# file through code, In this tutorial we are going to see how to add Layout and controls in XAML file.

In a Xamarin.Forms application, XAML is mostly used to define the visual contents of a page. A XAML file is always associated with a C# code file that provides code support for the markup. Together, these two files comprise a new class definition that includes child views and property initialization. Within the XAML file, classes and properties are referenced with XML elements and attributes, and links between the markup and code are established.

Lets’s Start:

Create a new solution and name it ”XAMLXamarinForms”.

Create a new ContentPageXaml file in this solution and name it as “XamlPage”.

xf13.1

You can see that there are 2 files created:
1) XamlPage.xaml.cs
2) XamlPage.xaml

• XamlPage.xaml have XAML tag that defines our layout for designing.
• XamlPage.xaml.cs is used for all the code part like Event handling, class and methods and varialbes etc.

xf13.2

Now go to XAMLXamarinForms.cs file and write:

Go to XamlPage.xaml.cs and write:

Now in XamlPage.xaml file:

We just add a simple label and display text in it. Now try to build and run the app you will see label.

Now we want to go one step ahead than just adding a label, add a button and handle it’s click event so we have some idea about how to events.

So, go into XamlPage.xaml file and write:

Now we can access button with the method “btn_clicked”. Write below code in XamlPage.xaml.cs:

That’s it. Now try to build and run the application.

For iOS:

xf13.3

xf13.4

For Android:

xf13.5

xf13.6

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="">