Xamarin.Forms XAML Basics
Getting Started with Cross-Platform Markup for Mobile Devices.
- PDF for offline use:
- Sample Code:
- Related Articles:
- Related Samples:
Let us know how you feel about this.
XAML—the eXtensible Application Markup Language—allows developers to define user interfaces in Xamarin.Forms applications using markup rather than code. XAML is never required in a Xamarin.Forms program, but it is often more succinct than equivalent code, more visually coherent, and potentially toolable. XAML is particularly well suited for use with the popular MVVM (Model-View-ViewModel) application architecture: XAML defines the View that is linked to ViewModel code through XAML-based data bindings.
XAML Basics Contents
- Part 1. Getting Started with XAML
- Part 2. Essential XAML Syntax
- Part 3. XAML Markup Extensions
- Part 4. Data Binding Basics
- Part 5. From Data Binding to MVVM
In addition to these XAML Basics articles, you can download preview chapters of our upcoming book Creating Mobile Apps with Xamarin.Forms. XAML topics are covered in more depth in many chapters, including:
Chapter 7. XAML vs. Code
Chapter 8. Code and XAML in Harmony
Chapter 10. XAML Markup Extensions
Chapter 18. MVVM
These chapters can be downloaded for free.
XAML is an XML-based language created by Microsoft as an alternative to programming code for instantiating and initializing objects, and organizing those objects in parent-child hierarchies. XAML has been adapted to several technologies within the .NET framework, but it has found its greatest utility in defining the layout of user interfaces within the Windows Presentation Foundation (WPF), Silverlight, and the Windows Runtime.
XAML is also part of Xamarin.Forms, the cross-platform natively-based programming interface for iOS, Android, and Windows Phone mobile devices. Using XAML, the Xamarin.Forms developer can define user interfaces using all the Xamarin.Forms views, layouts, and pages, as well as custom classes. The XAML file is parsed at build time to locate named objects, and again at runtime to instantiate and initialize objects, and to establish links between these objects and programming code.
XAML has several advantages over equivalent code:
- XAML is often more succinct and readable than equivalent code.
- The parent-child hierarchy inherent in XML allows XAML to mimic the parent-child hierarchy of user-interface objects with greater visual clarity.
- XAML can be easily hand-written by programmers, but also lends itself to be toolable and generated by visual design tools.
Of course, there are also disadvantages, mostly related to limitations that are intrinsic to markup languages:
- XAML cannot contain code. All event handlers must be defined in a code file.
- XAML cannot contain loops for repetitive processing. (However, several Xamarin.Forms visual objects—mostly notably
ListView—can generate multiple children based on the objects in its
- XAML cannot contain conditional processing (However, a data-binding can reference a code-based binding converter that effectively allows some conditional processing.)
- XAML generally cannot instantiate classes that do not define a parameterless constructor. (However there is sometimes a way around this restriction.)
- XAML generally cannot call methods. (Again, this restriction can sometimes be overcome.)
There is not yet a visual designer for generating XAML in Xamarin.Forms applications, so all XAML must be hand-written. Although the XAML editor in Xamarin Studio supplies some syntax suggestions and automatic completion, the XAML editor in Visual Studio does not. In addition, some XAML errors slip through the build process and are not revealed until runtime.
Programmers new to XAML might want to frequently build and run their applications, particularly after anything that might not be obviously correct. Even developers with lots of experience in XAML know that experimentation is rewarding.
XAML is basically XML, but XAML has some unique syntax features. The most important are:
- Property elements
- Attached properties
- Markup extensions
They are discussed in detail in the articles below, which conclude with an introduction to using XAML for implementing MVVM.
This article assumes a working familiarity with Xamarin.Forms. Reading An Introduction to Xamarin.Forms is highly recommended.
This article also assumes some familiarity with XML, including understanding the use of XML namespace declarations, and the terms element, tag, and attribute.
When you're familiar with Xamarin.Forms and XML, start reading Part 1. Getting Started with XAML.