An Introduction to Xamarin, Part 2: Xamarin.Forms

Introduction

Hello, and welcome to the second installment of my Xamarin article series. Before you continue reading this article, I would advise you to come to grips with “An Introduction to Xamarin, Part 1: Setup” first, if you haven’t already read it.

Xamarin.Forms

Xamarin.Forms is a cross-platform UI (User Interface) toolkit that allows developers to create native user interface layouts. These native user interface layouts can be shared across Android, iOS, and Universal Windows Platform (UWP) apps. User Interface (UI) definitions, as well as code, are shared across all platforms but rendered with native controls.

Xamarin.Forms apps use .NET Standard projects to:

  • Contain the shared code.
  • Separate the application projects that consume the shared code.
  • Build the output for each platform.

XAML

XAML (eXtensible Application Markup Language) is a declarative markup language that is used to define user interfaces. The UI (User Interface) is defined in an XML file using XAML syntax; runtime behavior events are defined in a separate .NET code-behind file.

XAML allows you to define user interfaces in Xamarin.Forms applications. XAML is mostly used with the Model-View-View Model (MVVM) application architecture. This means that XAML defines the View that is linked to View Model code via XAML-based data bindings.

Advantages of XAML

  • XAML is more readable than equivalent code.
  • The parent-child hierarchy inherent in XML allows XAML to mimic the parent-child hierarchy of user-interface objects.
  • XAML can be easily hand-written by programmers.

Disadvantages of XAML

  • All event handlers must be defined in a code file.
  • XAML cannot contain loops for repetitive tasks.
  • XAML cannot contain conditional processing.
  • XAML cannot instantiate a class.
  • XAML generally cannot call methods.

Views

Views are user-interface objects (controls or widgets) such as labels, buttons, and entry fields. The views supported by Xamarin.Forms all derive from the View class.

Views for Presentation

Views for presentation include:

  • Label
  • Image
  • BoxView
  • WebView
  • OpenGLView
  • Map

Views that Initiate Commands

These views include:

  • Button
  • Image Button
  • SearchBar

Views for Setting Values

These views include the following:

  • Slider
  • Stepper
  • Switch
  • DatePicker
  • TimePicker

Views for Editing Text

These views include:

  • Entry
  • Editor

Views to Indicate Activity

  • ActivityIndicator
  • ProgressBar

Views that Display Collections

These views include:

  • Picker
  • ListView
  • TableView

Layouts

Xamarin.Forms has several layouts to organize content on screen:

  • StackLayout is used to display views along a line that is either horizontal or vertical. Position and size within the layout are determined based on a view’s HeightRequest, WidthRequest, HorizontalOptions, and VerticalOptions.
  • FlexLayout is similar to StackLayout. The difference comes in if there are too many children to fit in a single row or column, FlexLayout can wrap the views inside it.
  • AbsoluteLayout is used to display views with size and position explicitly stated. Unlike StackLayout, AbsoluteLayout allows child views to overlap.
  • RelativeLayout is used to display views, with size and position specified as values relative to the values of the layout or another view.
  • Grid is used to display elements in rows and columns.

Conclusion

In this article, you got a basic understanding of Xamarin.Forms. In the next installment, you will make use of Layouts and Views productively. Until then, happy learning!

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read