Home » XAML for Xamarin.Forms

XAML for Xamarin.Forms

by Online Tutorials Library

XAML for Xamarin.Forms

Xaml or Extensible Markup Language is a declarative language which is used for creating the user interfaces in Xamarin.Forms.

Xaml is simple and declarative markup language which is based on XML. Xaml is case sensitive and strongly-typed markup language which separates the presentation from business logic. XAML elements are an XML representation of CLR objects. Xaml is used to create, initialize, and set the properties of an object in hierarchical relations. Xaml is mainly used for designing the UI in WPF, Silverlight, Windows Phone, and Xamarin Forms.

XAML working

Xaml files are converted into BAML (Binary Application Markup Language) which will be embedded as a resource into final DLL/exe.

XAML for Xamarin.Forms

Advantages of XAML

  • XAML is based on XML syntax.
  • XAML code is small and easier.
  • XAML code is easy to write and easy to understand as compared to code.
  • Designing the UI is easier with XAML as compared to code.
  • XAML provides a clear separation between UI(XAML) and UI logic(C#).
  • XAML separates the role of designer and developer.

Benefits of Xaml

Firstly, the biggest benefit of laying out the user interface in XAML is easy. When XAML is used to lay out the visual tree controls in the page then apps are made quickly and becomes very apparent.

The code in XAML is more readable. For Example: when control is data-bound to a property in a view model, XAML syntax is much cleaner than the C# syntax.

Xamarin DataBinding:

C# DataBinding:

XAML version becomes the more readable.

Finally, the Xamarin Introduces new version XAML previewer in both Xamarin Studio and Visual Studio. This tool allows the developer to view the user interface in IDE.

Structure of XAML page

Here, the opening <ContentPage> element, XAML parser knows that it has come across a content page and it should instantiate. Other UI controls are placed within the <ContentPage.Content> tags, in which the XAML parser instantiate to build the UI.

XAML parser is responsible for the working of the XAML file, finding the element that needs to be instantiated, setting their property, place the proper control in the appropriate place in the overall Layout, perform the data binding and so on, etc. It is an impressive technology.

Properties

In XAML, properties have two flavors. First One is Attribute Properties. These are the easiest and effective to understand. Attribute properties declared within the tag of the UI component. The attributes name will always be the same as the control’s property name, but the value of the attribute will always be a string.

We always specify a string because XAML accepts the rules of XML.

The second type of property of XAML is Element Property. Element Property is used when the value of the property is too complex to be expressed within a simple string.

Element properties tend to hide in plain sight. When the blank ContentPage is created, the element is an Element Property. This property is used to hold the rest of the content of the Page, which is too complex to express in the string.

Differences between the XAML and Code

XAML is easy to maintain and easy to modify than code. Xaml is easy to parsed and can be edited by software tools than code. XAML is sometimes more concise than the equivalent C# code.

XAML has no loops, no flow control, no algebric calculation syntax, and no event handlers. In this case, C# code helps us to define all these things.

File Structure

Xaml files are composed of two files. The UI and the code behind. If we want to create the Registration Form, then we would have the following:

  • Login.Xaml – XAML UI
  • Login.Xaml.CS- Code-behind for the UI

XAML UI Structure

XAML view is composed of a Page, different Layouts can be used, and without the Layout, several views are used to create the UI.

XAML for Xamarin.Forms


Next TopicXamarin Layout

You may also like