xamarin
  1. xamarin-forms-architecture

Xamarin Forms Architecture

Xamarin.Forms is a cross-platform UI toolkit that enables developers to create native user interface layouts that can be shared across iOS, Android, and Windows platforms. It provides a flexible API that allows developers to build a wide range of user interfaces while making it easier to target multiple platforms without duplicating code.

Overview

Xamarin.Forms architecture consists of two primary components - Page and Layouts, which are used to define the user interface for the application. Pages represent the screens of the application, and layouts define the structure of the user interface.

Pages are the visual elements of Xamarin.Forms that represent windows or screens. A page can contain multiple layouts such as stack, grid, or relative. The layouts can be nested within each other, creating complex user interfaces.

Layouts are containers that define the structure of user interface elements such as buttons, labels, and text boxes. Layouts enable developers to position and format these elements to create a visually appealing and functional user interface.

Page and Layouts

Pages

Pages are the fundamental building blocks of Xamarin.Forms applications, representing the user interface screens. Xamarin.Forms provides a range of page classes such as ContentPage, TabbedPage, CarouselPage, and MasterDetailPage, which can be used to create different types of user interfaces.

ContentPage

The ContentPage class is the primary page container for the application, representing a single screen of content. It contains a single content view that occupies the complete screen area.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SampleApp.MainPage">
    <Label Text="Welcome to Xamarin.Forms!"
           VerticalOptions="CenterAndExpand" 
           HorizontalOptions="CenterAndExpand" />
</ContentPage>

TabbedPage

The TabbedPage class represents pages with multiple tabs, each containing a different page. The user can navigate between tabs by swiping left or right.

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="SampleApp.MainPage">
  <ContentPage Title="Tab 1">
    <Label Text="TabbedPage 1 Content" />
  </ContentPage>
  <ContentPage Title="Tab 2">
    <Label Text="TabbedPage 2 Content" />
  </ContentPage>
</TabbedPage>

CarouselPage

The CarouselPage class is a page container with a horizontal swipe gesture to navigate between pages. The pages can be different, and the user can swipe left or right to move to the next or previous page.

<?xml version="1.0" encoding="utf-8" ?>
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SampleApp.MainPage">
  <ContentPage>
    <Label Text="Carousel Page 1 Content" />
  </ContentPage>
  <ContentPage>
    <Label Text="Carousel Page 2 Content" />
  </ContentPage>
  <ContentPage>
    <Label Text="Carousel Page 3 Content" />
  </ContentPage>
</CarouselPage>

MasterDetailPage

The MasterDetailPage class is a page container used to implement a master-detail UI pattern. It consists of two pages - a master page and a detail page. The master page list contains items, and when the user taps an item, the detail page displays the details for that item.

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
                  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                  x:Class="SampleApp.MainPage">
  <MasterDetailPage.Master>
    <ContentPage Title="Menu">
      <ListView x:Name="listView">
        <ListView.ItemTemplate>
          <DataTemplate>
            <TextCell Text="{Binding Title}" />
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
    </ContentPage>
  </MasterDetailPage.Master>
  <MasterDetailPage.Detail>
    <NavigationPage>
      <ContentPage>
        <Label Text="Detail Page" />
      </ContentPage>
    </NavigationPage>
  </MasterDetailPage.Detail>
</MasterDetailPage>

Layouts

Layouts are containers that define the structure of user interface elements such as buttons, labels, and text boxes used in pages. Layouts enable developers to position and format these elements to create a visually appealing and functional user interface. Xamarin.Forms provides various layout classes such as stack, grid, and relative, which can be used to create the user interface.

StackLayout

The StackLayout class is a layout container that arranges child elements vertically or horizontally. By default, it stacks the child elements vertically.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SampleApp.MainPage">
    <StackLayout>
        <Label Text="Item 1" />
        <Label Text="Item 2" />
        <Label Text="Item 3" />
    </StackLayout>
</ContentPage>

Grid

The Grid class is a layout container used to organize child elements in a grid format with rows and columns. Rows and columns can be set to specific sizes or span multiple rows or columns.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SampleApp.MainPage">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="2*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>
        <Label Text="Row 0, Col 0" Grid.Row="0" Grid.Column="0" />
        <Label Text="Row 1, Col 1 (span 2)" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" />
    </Grid>
</ContentPage>

RelativeLayout

The RelativeLayout class is a layout container that enables child elements to be aligned and positioned relative to each other or the parent element using constraints.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SampleApp.MainPage">
    <RelativeLayout>
        <Label Text="Aligned to parent top" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=0}" />
        <Label Text="Aligned to parent center and 50px from top" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=.5}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=X, Factor=.5}" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.25}" />
        <Label Text="Aligned to label1 center and 50px to the left" RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, Property=Y, ElementName=label1, Factor=1}" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView, Property=Width, ElementName=label1, Factor=-0.5}" />
    </RelativeLayout>
</ContentPage>

Summary

Xamarin.Forms provides a flexible architecture that allows developers to create native user interfaces for iOS, Android, and Windows platforms using a single codebase. Pages and layouts are the primary building blocks of Xamarin.Forms applications, enabling developers to create visually appealing and functional user interfaces. Xamarin.Forms provides a range of page and layout classes to choose from to create different types of user interfaces that can be shared across different platforms. By using Xamarin.Forms architecture, developers can build cross-platform applications faster, more efficiently, and with less code.

Published on: