Partajați prin


Xamarin.Forms XAML Basics

The eXtensible Application Markup Language (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, the Windows Runtime, and the Universal Windows Platform (UWP).

XAML 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 and more visually coherent than equivalent code, and potentially toolable. XAML is 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.

Within a XAML file, 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 can be either compiled or embedded in the executable. Either way, the XAML information 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 with greater visual clarity the parent-child hierarchy of user-interface objects.
  • XAML can be easily hand-written by programmers, but also lends itself to be toolable and generated by visual design tools.

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—most notably ListView —can generate multiple children based on the objects in its ItemsSource collection.)
  • 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. All XAML must be hand-written, but you can use XAML Hot Reload in Visual Studio 2019 or Visual Studio for Mac to view your screen designs as you edit them. 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

These features are not XML extensions. XAML is entirely legal XML. But these XAML syntax features use XML in unique ways. They are discussed in detail in the articles below, which conclude with an introduction to using XAML for implementing MVVM.

Requirements

This article assumes a working familiarity with Xamarin.Forms. 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.

Find more Xamarin videos on Channel 9 and YouTube.