.Net Maui Layout Controls Stacklayout Grid Complete Guide
Understanding the Core Concepts of .NET MAUI Layout Controls StackLayout, Grid
.NET MAUI Layout Controls: StackLayout and Grid
StackLayout
Definition & Purpose:
StackLayout
arranges its children in a single vertical stack or a single horizontal stack based on the Orientation
property. This layout is simple and efficient for creating straightforward and sequential layouts, such as a series of labels or buttons. StackLayout
is ideal for aligning elements in a single direction, either vertically or horizontally, without overlapping.
Key Properties & Important Features:
- Orientation: Determines whether children are stacked vertically (
Vertical
) or horizontally (Horizontal
). - Spacing: Defines space between each child element.
- VerticalOptions/HorizontalOptions: Control how space is allotted and aligned relative to the available space.
- Padding: Adds space around the StackLayout.
- Device-Specific Settings: Modify the appearance and behavior according to platform-specific conditions.
Example:
<StackLayout VerticalOptions="FillAndExpand" Spacing="10">
<Label Text="Header" FontSize="Large" FontAttributes="Bold" />
<Label Text="This is a sample description." />
<Button Text="Submit" />
</StackLayout>
Advantages:
- Simplicity: Easy to use and understand.
- Performance: High performance for simple layouts.
- Flexibility: Can adapt to different screen sizes with proper settings.
Grid
Definition & Purpose:
Grid
provides a table-based layout system where elements are placed in rows and columns, allowing more complex arrangements compared to StackLayout
. Grid
is highly versatile and supports the definition of column and row sizes, as well as complex child element positioning through attachable properties.
Key Properties & Important Features:
- Row/Column Definitions: Specify the number and size of rows and columns using
RowDefinition
andColumnDefinition
. - Rows/Columns Properties: Access and manipulate rows and columns after Grid has been created.
- Attachable Properties:
Grid.Row
,Grid.Column
,Grid.RowSpan
,Grid.ColumnSpan
position elements within the Grid. - Star Sizing Units: Allows rows and columns to grow proportionally based on available space.
- Alignment Options: Advanced alignment controls throttle individual element placement within cells.
- Padding: Adjusts space around the Grid, as well as on individual elements within cells.
Example:
<Grid RowDefinitions="Auto,*" ColumnDefinitions="*,*">
<!-- Header row -->
<Label Text="Main Title" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" FontSize="Large" FontAttributes="Bold" />
<!-- Content sections -->
<Label Text="Left Column Content" Grid.Row="1" Grid.Column="0" />
<Label Text="Right Column Content" Grid.Row="1" Grid.Column="1" />
</Grid>
Advantages:
- Complexity: Handles more intricate layouts than
StackLayout
. - Precision Control: Allows granular control over the position and size of child elements.
- Adaptability: Efficient across a wide spectrum of device resolutions and orientations.
Comparison and Use Cases
StackLayout:
- Best for linear arrangements (vertical or horizontal).
- Ideal for simple and straightforward configurations.
- Beneficial for rapidly prototyping and non-complex interfaces.
Grid:
- Suitable for complex and multi-dimensional layouts.
- Highly adaptable for both simple and intricate designs.
- Excellent choice for professional-grade, polished user interfaces.
Online Code run
Step-by-Step Guide: How to Implement .NET MAUI Layout Controls StackLayout, Grid
Example 1: Using a StackLayout
Objective: Create a simple interface with a vertical StackLayout
containing three labels and a button.
Create a new .NET MAUI project:
- Open Visual Studio.
- Create a new project and select ".NET MAUI App".
- Name your project and click "Create".
Modify the MainPage.xaml file:
- Open
MainPage.xaml
.
- Open
Add the StackLayout and its children:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MauiApp1.MainPage" Title="StackLayout Example"> <StackLayout Margin="20"> <Label Text="Welcome to .NET MAUI!" FontSize="Title" HorizontalOptions="Center" VerticalOptions="CenterAndExpand"/> <Label Text="This is a simple example of a StackLayout." FontSize="Medium" HorizontalOptions="Center" VerticalOptions="Start"/> <Button Text="Click Me" HorizontalOptions="Center" VerticalOptions="EndAndExpand"/> </StackLayout> </ContentPage>
Explanation:
StackLayout
places its child elements either horizontally or vertically. In this example, we are stacking them vertically (default behavior).- Each
Label
andButton
has theHorizontalOptions
set toCenter
, so they align in the middle horizontally. - The first
Label
usesVerticalOptions="CenterAndExpand"
which expands it to take up remaining space while centering it vertically within the parent. - The second
Label
usesVerticalOptions="Start"
which aligns it at the top within the available space. - The
Button
usesVerticalOptions="EndAndExpand"
which pushes it to the bottom while taking up the remaining space.
Run the application:
- Click on the run button in Visual Studio to execute the application.
- You should see three centered elements in a single column.
Example 2: Using a Grid
Objective: Create a more complex interface with a Grid
that contains two rows and three columns, demonstrating various alignment and spacing options.
Modify the MainPage.xaml file:
- Open
MainPage.xaml
.
- Open
Add the grid and populate it:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MauiApp1.MainPage" Title="Grid Example"> <Grid Margin="20" RowSpacing="15" ColumnSpacing="5"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Text="First Name:" HorizontalTextAlignment="End" VerticalTextAlignment="Center"/> <Entry Grid.Row="0" Grid.Column="1" Placeholder="Enter First Name"/> <Label Grid.Row="0" Grid.Column="2" Text="Last Name:" HorizontalTextAlignment="End" VerticalTextAlignment="Center"/> <Entry Grid.Row="1" Grid.Column="0" Placeholder="Enter First Name"/> <Label Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Text="This is an entry field for Last Name." VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/> </Grid> </ContentPage>
Explanation:
- The
Grid
is divided into two rows and three columns. - The first row (
Grid.Row="0"
) has two cells with labels and entries for First Name and Last Name.- The first label is aligned to the right and vertically centered using
HorizontalTextAlignment="End"
andVerticalTextAlignment="Center"
. - There’s an entry field next to the First Name label where users can type their first name.
- The last name label is also aligned to the right and vertically centered.
- The first label is aligned to the right and vertically centered using
- The second row (
Grid.Row="1"
) occupies the entire width of the first column and includes another entry field. - Below the second entry field, there’s a third label spanning across two columns (
Grid.ColumnSpan="2"
) displaying instructional text. - The
RowSpacing
andColumnSpacing
control the amount of space between the grid cells.
- The
Run the application:
- Click on the run button in Visual Studio to execute the application.
- You should see a two-row layout with three columns. Each label and entry takes up appropriate spaces based on the properties provided.
Login to post a comment.