Xamarin.Forms Grid Tutorial
Before attempting this tutorial, you should have successfully completed the:
- Build your first Xamarin.Forms app quickstart.
In this tutorial, you learn how to:
- Create a Xamarin.Forms
Grid
in XAML. - Specify columns and rows for the
Grid
. - Span content across multiple columns or multiple rows in the
Grid
.
You will use Visual Studio 2019, or Visual Studio for Mac, to create a simple application that demonstrates how to layout controls within a Grid
. The following screenshots show the final application:
You'll also use XAML Hot Reload for Xamarin.Forms to see UI changes without rebuilding your application.
Create a grid
A Grid
is a layout that organizes its children into rows and columns, which can have proportional or absolute sizes. By default, a Grid
contains one row and one column.
To complete this tutorial you should have Visual Studio 2019 (latest release), with the Mobile development with .NET workload installed. In addition, you will require a paired Mac to build the tutorial application on iOS. For information about installing the Xamarin platform, see Installing Xamarin. For information about connecting Visual Studio 2019 to a Mac build host, see Pair to Mac for Xamarin.iOS development.
Launch Visual Studio, and create a new blank Xamarin.Forms app named GridTutorial.
Important
The C# and XAML snippets in this tutorial requires that the solution is named GridTutorial. Using a different name will result in build errors when you copy code from this tutorial into the solution.
For more information about the .NET Standard library that gets created, see Anatomy of a Xamarin.Forms application in the Xamarin.Forms Quickstart Deep Dive.
In Solution Explorer, in the GridTutorial project, double-click MainPage.xaml to open it. Then, in MainPage.xaml, remove all of the template code and replace it with the following code:
<?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="GridTutorial.MainPage"> <Grid Margin="20,35,20,20"> <Label Text="The Grid has its Margin property set, to control the rendering position of the Grid." /> </Grid> </ContentPage>
This code declaratively defines the user interface for the page, which consists of a
Label
in aGrid
. By default, theGrid
positions its child views in a single location. Therefore, aGrid
that contains multiple children should specify columns and rows, which will be covered in the next exercise. In addition, theMargin
property indicates the rendering position of theGrid
within theContentPage
.Note
In addition to the
Margin
property, thePadding
property can also be set on aGrid
. ThePadding
property value specifies the distance between the bounds of theGrid
and its children. For more information, see Margin and Padding.In the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen iOS remote simulator or Android emulator:
For more information about the
Grid
, see Xamarin.Forms Grid.
Specify columns and rows
In MainPage.xaml, modify the
Grid
declaration to define columns and rows, and place content in the columns and rows:<Grid Margin="20,35,20,20"> <Grid.ColumnDefinitions> <ColumnDefinition Width="0.5*" /> <ColumnDefinition Width="0.5*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="50" /> <RowDefinition Height="50" /> </Grid.RowDefinitions> <Label Text="Column 0, Row 0" /> <Label Grid.Column="1" Text="Column 1, Row 0" /> <Label Grid.Row="1" Text="Column 0, Row 1" /> <Label Grid.Column="1" Grid.Row="1" Text="Column 1, Row 1" /> </Grid>
This code defines columns and rows for the
Grid
, and positionsLabel
instances in specific columns and rows. The column and row data is stored in theColumnDefinitions
andRowDefinitions
properties, which are collections ofColumnDefinition
andRowDefinition
objects, respectively. The width of eachColumnDefinition
is set by theColumnDefinition.Width
property, and the height of eachRowDefinition
is set by theRowDefinition.Height
property. Valid width and height values are:Auto
, which sizes the column or row to fit the content.- Proportional values, which size the columns and rows as a proportion of the remaining space. Proportional values are indicated by ending with a
*
. - Absolute values, which size the columns or rows with specific, fixed values.
Therefore, in the code above, each column has a width of half the
Grid
, while each row has a height of 50 device-independent units.The position of each
Label
in theGrid
is specified with theGrid.Column
andGrid.Row
attached properties, using a zero-based index. Therefore, the first column is column 0, and the first row is row 0. The firstLabel
lacks these attached properties, because any child views that don't set them will automatically be rendered in column 0, row 0.Note
The spacing between columns and rows in a
Grid
can be set with theColumnSpacing
andRowSpacing
properties. For more information, see Spacing in the Xamarin.Forms Grid guide.If the application is still running, save the changes to the file and the application user interface will automatically be updated in your simulator or emulator. Otherwise, in the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator:
Span multiple columns or rows
In MainPage.xaml, modify the
Grid
declaration to define columns and rows, and place content that spans the columns and rows:<Grid Margin="20,35,20,20"> <Grid.ColumnDefinitions> <ColumnDefinition Width="0.5*" /> <ColumnDefinition Width="0.5*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="50" /> <RowDefinition Height="30" /> <RowDefinition Height="30" /> </Grid.RowDefinitions> <Label Grid.ColumnSpan="2" Text="This text uses the ColumnSpan property to span both columns." /> <Label Grid.Row="1" Grid.RowSpan="2" Text="This text uses the RowSpan property to span two rows." /> </Grid>
This code defines columns and rows for the
Grid
, and positionsLabel
instances in specific columns and rows. The firstLabel
sets theColumnSpan
attached property so that its text spans multiple columns. TheColumnSpan
property is set to 2, which represents the number of columns theLabel
will span. The secondLabel
sets theRowSpan
attached property so that its text spans multiple rows. TheRowSpan
property is set to 2, which represents the number of rows that theLabel
will span.If the application is still running, save the changes to the file and the application user interface will automatically be updated in your simulator or emulator. Otherwise, in the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen iOS remote simulator or Android emulator:
In Visual Studio, stop the application.
For more information about spanning columns and rows, see Rows and columns in the Xamarin.Forms Grid guide.
Congratulations!
Congratulations on completing this tutorial, where you learned how to:
- Create a Xamarin.Forms
Grid
in XAML. - Specify columns and rows for the
Grid
. - Span content across multiple columns or multiple rows in the
Grid
.
Next steps
To learn more about the basics of creating mobile applications with Xamarin.Forms, continue to the CollectionView tutorial.
Related links
Have an issue with this section? If so, please give us some feedback so we can improve this section.