.NET MAUI Layout Controls: StackLayout and Grid
When developing applications using .NET Multi-platform App UI (MAUI), layout controls serve a crucial role in crafting the user interface. Among the various layout options, StackLayout
and Grid
are two fundamental and highly versatile layout controls that help in organizing and aligning child elements efficiently. This article will delve into the details of these controls, illustrating their functionalities, usage, and importance in application design.
StackLayout
Overview:
StackLayout
is a simple yet effective layout control that arranges its child elements sequentially in a single column or row. It is a unidirectional layout, meaning elements are stacked either vertically (default) or horizontally based on the Orientation
property.
Key Properties:
Orientation
: Defines the direction in which elements are arranged (Vertical
orHorizontal
).Spacing
: Specifies the space between elements in the layout.HorizontalOptions
andVerticalOptions
: Control spacing and alignment of child elements relative to the layout.Padding
: Adds space around the layout itself.
Example:
Below is a simple example of StackLayout
arranging elements vertically. The elements include a label, an entry, and a button.
<StackLayout Orientation="Vertical" Spacing="10" Padding="10">
<Label Text="Enter Name" />
<Entry Placeholder="Your Name" />
<Button Text="Submit" />
</StackLayout>
In the above example, setting the Orientation
to Vertical
arranges the label, entry, and button in a column. The Spacing
property provides 10 units of space between these elements, and Padding
adds space around the entire stack.
Use Cases:
- Simple Form Layout: Ideal for creating forms where elements are arranged linearly.
- Lists and Menus: Suitable for creating simple lists or menus where items need to be stacked vertically or horizontally.
- Navigation Bars: Commonly used for navigation bars where elements like buttons are aligned side by side.
Benefits:
- Simplicity: Easy to understand and implement, making it a good choice for straightforward designs.
- Flexibility: Allows for dynamic stacking of elements, which can be controlled programmatically.
- Performance: Generally performs well due to its simple structure.
Grid
Overview:
Grid
is a powerful and flexible layout control that divides the layout into rows and columns, providing a grid-like arrangement for child elements. This makes it suitable for creating complex and responsive user interfaces.
Key Properties:
RowDefinitions
andColumnDefinitions
: Define the number and size of rows and columns.RowSpacing
andColumnSpacing
: Control the spacing between rows and columns.Padding
: Adds space around the grid.HorizontalOptions
andVerticalOptions
: Control spacing and alignment of elements.
Example:
The following example demonstrates a Grid
layout with two rows and three columns, containing labels, entries, and buttons.
<Grid RowSpacing="10" ColumnSpacing="10" Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Text="Name:" Grid.Row="0" Grid.Column="0" />
<Entry Placeholder="Your Name" Grid.Row="0" Grid.Column="1" />
<Button Text="Submit" Grid.Row="1" Grid.Column="1" HorizontalOptions="Center" />
<Label Text="Email:" Grid.Row="0" Grid.Column="2" />
<Entry Placeholder="Your Email" Grid.Row="1" Grid.Column="2" />
</Grid>
In this example, RowDefinitions
and ColumnDefinitions
specify the layout structure. Grid.Row
and Grid.Column
properties determine the cell position of each child element within the grid.
Use Cases:
- Forms Layouts: Ideal for creating detailed forms where elements need to be aligned in a structured grid.
- Responsive Design: Helps in creating layouts that adjust dynamically based on screen size and orientation.
- Dashboard Layouts: Suitable for creating dashboards where data points and controls need to be organized in a grid format.
Benefits:
- Structure: Provides a powerful and flexible structure for complex layouts.
- Responsiveness: Easier to create responsive designs that adapt to different screen sizes.
- Advanced Alignment: Offers fine-grained control over the positioning and alignment of elements.
Conclusion
StackLayout
and Grid
are essential layout controls in .NET MAUI, each serving different purposes and providing unique benefits. StackLayout
is ideal for simple linear arrangements due to its simplicity and ease of use. On the other hand, Grid
offers more advanced and flexible layouts, making it suitable for creating complex and responsive user interfaces.
By understanding and effectively utilizing these layout controls, developers can create visually appealing and functional applications that deliver a great user experience across multiple platforms.
Step-by-Step Guide to Using .NET MAUI Layout Controls: StackLayout and Grid for Beginners
Introduction
.NET MAUI (Multi-platform App UI) is a powerful framework for building native mobile, desktop, and web applications from a single codebase, shared across platforms. One of the fundamental aspects of .NET MAUI is understanding how to use layout controls to arrange UI elements. This guide will walk you through setting up a project, designing a simple UI using StackLayout
and Grid
controls, and understanding the data flow in a .NET MAUI application.
Prerequisites
- Install the latest version of Visual Studio with .NET MAUI support.
- Basic knowledge of C# and XAML.
Step 1: Set Up Your Project
- Launch Visual Studio: Start Visual Studio and create a new project.
- Select Template: Choose ".NET MAUI App" from the project templates.
- Configure Project: Name your project, choose a location, and click "Create".
- Create Solution: Select the appropriate options (such as including unit tests) and click "Create".
Step 2: Designing the UI with StackLayout and Grid
Let's create a simple user interface using both StackLayout
and Grid
to understand how they work.
Open MainPage.xaml: This is the main XAML file where you'll design your application's UI.
Basic Structure:
<?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="YourAppNamespace.MainPage"> <StackLayout Padding="10"> </StackLayout> </ContentPage>
- Here, we use
StackLayout
to arrange child elements in a single line, either vertically or horizontally.
- Here, we use
Using StackLayout:
Vertical Stack (Default):
<StackLayout Padding="10" Spacing="10"> <Label Text="Welcome to .NET MAUI" FontSize="Large" HorizontalOptions="Center" /> <Entry Placeholder="Enter your name" /> <Button Text="Submit" BackgroundColor="LightBlue" /> </StackLayout>
- Elements are stacked vertically with the specified spacing.
Horizontal Stack:
<StackLayout Padding="10" Spacing="10" Orientation="Horizontal"> <Label Text="Item 1" /> <Label Text="Item 2" /> <Label Text="Item 3" /> </StackLayout>
- Elements are aligned horizontally.
Using Grid:
Grid
divides the screen into rows and columns to arrange elements.- Basic Grid Structure:
<Grid Padding="10" RowSpacing="10" ColumnSpacing="10"> <!-- Define Rows and Columns --> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <!-- Place Elements --> <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Text="Welcome to .NET MAUI" FontSize="Large" HorizontalOptions="Center" /> <Label Grid.Row="1" Grid.Column="0" Text="Name:" /> <Entry Grid.Row="1" Grid.Column="1" Placeholder="Enter your name" /> <Button Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Text="Submit" BackgroundColor="LightBlue" /> </Grid>
- The
Grid
defines two rows and two columns. RowDefinition
andColumnDefinition
determine the layout structure.- The
Grid.Row
andGrid.Column
properties specify the position of elements within the grid. Grid.RowSpan
andGrid.ColumnSpan
allow elements to span across multiple rows or columns.
- The
Step 3: Run the Application
- Build the Project: Press
Ctrl + Shift + B
or selectBuild
>Build Solution
from the menu. - Run the Project: Press
F5
to start debugging the application on the default selected platform (e.g., Windows, Android, iOS). - Verify the UI:
- You should see a screen with a label, entry box, and button arranged in a
StackLayout
orGrid
layout. - Experiment with different layouts to see how the UI changes.
- You should see a screen with a label, entry box, and button arranged in a
Step 4: Data Flow in .NET MAUI
Understanding how data flows within a .NET MAUI application is crucial for building interactive apps.
Create a ViewModel:
Create a new class file named
MainViewModel.cs
:using System.ComponentModel; using System.Windows.Input; using Microsoft.Maui.Controls; namespace YourAppNamespace { public class MainViewModel : INotifyPropertyChanged { private string _name; public string Name { get => _name; set { _name = value; OnPropertyChanged(nameof(Name)); } } public ICommand SubmitCommand { get; } public MainViewModel() { SubmitCommand = new Command(OnSubmit); } private void OnSubmit() { // Handle the submit action Application.Current.MainPage.DisplayAlert("Alert", $"Hello, {Name}!", "OK"); } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }
Bind ViewModel to View:
Update
MainPage.xaml
to bind to theMainViewModel
:<?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="YourAppNamespace.MainPage" xmlns:viewModels="clr-namespace:YourAppNamespace" x:DataType="viewModels:MainViewModel"> <Grid Padding="10" RowSpacing="10" ColumnSpacing="10"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Text="Welcome to .NET MAUI" FontSize="Large" HorizontalOptions="Center" /> <Label Grid.Row="1" Grid.Column="0" Text="Name:" /> <Entry Grid.Row="1" Grid.Column="1" Placeholder="Enter your name" Text="{Binding Name}" /> <Button Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Text="Submit" BackgroundColor="LightBlue" Command="{Binding SubmitCommand}" /> </Grid> </ContentPage>
Set the
BindingContext
inMainPage.xaml.cs
:public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); BindingContext = new MainViewModel(); } }
Run the Application:
- Press
F5
to run the application. - Enter a name in the entry box and click the "Submit" button; an alert will display with the entered name.
- Press
Conclusion
In this guide, you learned how to set up a .NET MAUI project, design a simple UI using StackLayout
and Grid
layout controls, and understand the data flow in a .NET MAUI application. Experimenting with different layouts and data bindings will help you master .NET MAUI and build more complex applications. Happy coding!
Top 10 Questions and Answers: .NET MAUI Layout Controls - StackLayout and Grid
1. What are the key differences between StackLayout and Grid in .NET MAUI?
Answer:
StackLayout
and Grid
are both fundamental layout controls in .NET MAUI, but they serve different purposes and have distinct characteristics.
StackLayout: This layout stacks child elements either vertically or horizontally, depending on the
Orientation
property. It is straightforward and useful for simple layouts where elements need to be arranged in a linear fashion.StackLayout
respects the size request of each child element, meaning elements can have varying sizes.Grid: This layout organizes child elements into rows and columns, providing a more flexible and complex arrangement than
StackLayout
. You define rows and columns explicitly, and then place elements in specific cells.Grid
is ideal for creating complex user interfaces with precise controls over element positioning and sizing.
2. How do you create a simple vertical StackLayout in .NET MAUI?
Answer:
To create a simple vertical StackLayout
in .NET MAUI
, you can define the StackLayout
in XAML or in code-behind. Here is an example using XAML:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyMauiApp.MainPage">
<StackLayout
Padding="10"
Spacing="10"
BackgroundColor="LightGray">
<Label Text="Welcome to .NET MAUI!"
FontSize="24"
TextColor="Black" />
<Button Text="Click Me"
BackgroundColor="Green"
TextColor="White" />
<Entry Placeholder="Enter Text Here" />
</StackLayout>
</ContentPage>
In this example, a StackLayout
with a vertical orientation (default) is defined. It contains a Label
, a Button
, and an Entry
control, each of which is stacked vertically with a padding of 10 and a spacing between elements of 10.
3. Can elements in a StackLayout have different sizes?
Answer:
Yes, elements within a StackLayout
can have different sizes. Each child of a StackLayout
can have its own width and height defined via properties such as WidthRequest
and HeightRequest
. The StackLayout
respects these requests and adjusts its layout accordingly.
Here's an example:
<StackLayout Padding="10"
Spacing="10">
<Label Text="Label with smaller height"
HeightRequest="30"
BackgroundColor="LightBlue" />
<Label Text="Label with larger height"
HeightRequest="50"
BackgroundColor="LightGreen" />
</StackLayout>
In this code, two labels with different HeightRequest
values are stacked vertically.
4. How do you create a Grid layout with two rows and two columns in .NET MAUI?
Answer:
Creating a Grid
with two rows and two columns in .NET MAUI
involves defining the rows and columns using RowDefinitions
and ColumnDefinitions
, and then placing controls within the specified cells. Here is an example of how to do this in XAML:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyMauiApp.MainPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Text="Label 1"
BackgroundColor="LightBlue"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Grid.Row="0"
Grid.Column="0" />
<Label Text="Label 2"
BackgroundColor="LightGreen"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Grid.Row="0"
Grid.Column="1" />
<Label Text="Label 3"
BackgroundColor="LightPink"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Grid.Row="1"
Grid.Column="0" />
<Label Text="Label 4"
BackgroundColor="LightYellow"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Grid.Row="1"
Grid.Column="1" />
</Grid>
</ContentPage>
In this example, a Grid
with two rows and two columns is defined. Each label is placed in a specific cell using the Grid.Row
and Grid.Column
properties.
5. How do you align items within cells in a Grid layout?
Answer:
In a Grid
layout, you can align items horizontally and vertically within cells using the HorizontalOptions
and VerticalOptions
properties. These properties can be set to various values such as Start
, Center
, End
, Fill
, FillAndExpand
, etc.
Here’s an example of aligning different labels within the same cell:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Text="Left Aligned"
HorizontalOptions="Start"
VerticalOptions="Center"
Grid.Row="0"
Grid.Column="0" />
<Label Text="Center Aligned"
HorizontalOptions="Center"
VerticalOptions="Center"
Grid.Row="0"
Grid.Column="0" />
<Label Text="Right Aligned"
HorizontalOptions="End"
VerticalOptions="Center"
Grid.Row="0"
Grid.Column="0" />
</Grid>
In this example, three labels are added to the same cell in a grid, but they are aligned differently using the HorizontalOptions
property.
6. What is the difference between Auto
, *
, and specific widths/heights in Grid layout definitions?
Answer:
In a Grid
layout, the Height
and Width
properties of RowDefinition
and ColumnDefinition
can be set to different types of values to control the layout behavior:
Auto: The row or column will take up only as much space as needed by its content. If a row or column is set to
Auto
, it will size itself based on the size of the largest element within it.Star (*): Rows or columns with a star width or height are considered proportional. Their size is relative to the sum of all star values. For example, if you have two columns with
Width="*"
and one withWidth="2*"
in the sameGrid
, the third column will be twice as wide as the other two. If set to just*
, all columns will be of equal width.Specific Width or Height: You can set a specific pixel width or height for a row or column, which ensures that it will be exactly that size.
Here’s an example illustrating these concepts:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Label Text="Auto Row"
BackgroundColor="LightBlue"
Grid.Row="0"
Grid.Column="0" />
<Label Text="Star Row"
BackgroundColor="LightGreen"
Grid.Row="1"
Grid.Column="0" />
<Label Text="Specific Height Row"
BackgroundColor="LightPink"
Grid.Row="2"
Grid.Column="0" />
<Label Text="First Star Column"
BackgroundColor="LightYellow"
Grid.Row="0"
Grid.Column="1" />
<Label Text="Second Star Column"
BackgroundColor="LightGray"
Grid.Row="1"
Grid.Column="1" />
</Grid>
- The first row will be as tall as the tallest label in it.
- The second row will take up the remaining space (after the first and third rows).
- The third row will be exactly 100 pixels tall.
- The first column will take up 1/3 of the
Grid
’s width. - The second column will take up 2/3 of the
Grid
’s width.
7. How can you combine StackLayout and Grid for complex layouts in .NET MAUI?
Answer:
Combining StackLayout
and Grid
can provide the flexibility needed for creating complex user interfaces in .NET MAUI. You can place StackLayout
inside a Grid
or vice versa to achieve a balanced layout.
Here is an example of combining StackLayout
and Grid
:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- Header Section -->
<Label Text="Header"
BackgroundColor="LightGray"
FontSize="24"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Grid.Row="0"
Grid.Column="0" />
<!-- Body Section with StackLayout -->
<StackLayout Grid.Row="1"
Grid.Column="0"
Padding="10"
Spacing="10">
<Label Text="First Entry"
BackgroundColor="LightBlue" />
<Entry Placeholder="Value 1" />
<Label Text="Second Entry"
BackgroundColor="LightGreen" />
<Entry Placeholder="Value 2" />
</StackLayout>
<!-- Footer Section -->
<Label Text="Footer"
BackgroundColor="LightGray"
FontSize="24"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Grid.Row="2"
Grid.Column="0" />
</Grid>
In this example, the Grid
defines the layout of the header, body, and footer sections. The body section uses a StackLayout
to stack input fields and labels vertically, demonstrating how you can nest layouts for more structured design.
8. How do you handle orientation changes in StackLayout and Grid?
Answer:
Handling orientation changes in .NET MAUI
involves ensuring that your layouts respond correctly to changes from portrait to landscape and back. Both StackLayout
and Grid
can be configured to adapt to these changes using responsive design techniques.
For StackLayout:
You can set the Orientation
property dynamically based on the device's orientation.
<StackLayout x:Name="myStackLayout"
Padding="10"
Spacing="10">
<Label Text="Orientation Example"
FontSize="24"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Entry Placeholder="Enter Text" />
<Button Text="Click Me" />
</StackLayout>
In code-behind, you can handle the orientation change:
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
myStackLayout.Orientation = width < height ? StackOrientation.Vertical : StackOrientation.Horizontal;
}
For Grid:
Grid cells can grow or shrink based on the star (*
) sizing, which makes it inherently responsive.
<Grid RowSpacing="10"
ColumnSpacing="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Label Text="Grid Example"
Grid.Row="0"
Grid.Column="0"
FontSize="24"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Entry Placeholder="Value 1"
Grid.Row="1"
Grid.Column="0" />
<Entry Placeholder="Value 2"
Grid.Row="1"
Grid.Column="1" />
</Grid>
In this example, the grid layout will adjust automatically as the device changes orientation, thanks to the star sizing.
9. How do you create nested layouts in .NET MAUI?
Answer:
Nested layouts refer to placing one layout inside another. This technique allows for complex and flexible UI designs. Here’s an example of how to create nested layouts in .NET MAUI
using StackLayout
and Grid
.
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyMauiApp.MainPage">
<StackLayout Padding="10"
Spacing="10"
BackgroundColor="LightGray">
<Label Text="Nested Layout Example"
FontSize="24"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Grid RowSpacing="10"
ColumnSpacing="10"
BackgroundColor="LightBlue">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Label Text="Grid in StackLayout"
Grid.Row="0"
Grid.ColumnSpan="2"
FontSize="18"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Entry Placeholder="Field 1"
Grid.Row="1"
Grid.Column="0" />
<Entry Placeholder="Field 2"
Grid.Row="1"
Grid.Column="1" />
</Grid>
<StackLayout Orientation="Horizontal"
Padding="10"
Spacing="10"
BackgroundColor="LightGreen">
<Label Text="Horizontal Stack in StackLayout"
VerticalOptions="Center" />
<Button Text="Button 1"
VerticalOptions="Center" />
<Button Text="Button 2"
VerticalOptions="Center" />
</StackLayout>
</StackLayout>
</ContentPage>
In this example, a StackLayout
contains a Grid
and another StackLayout
. The Grid
and the horizontal StackLayout
each have their own child elements, demonstrating how to create structured user interfaces using nested layouts.
10. How do you add spacing and padding to Layout Controls in .NET MAUI?
Answer:
Adding spacing between elements and padding around elements are essential for creating well-balanced and visually appealing layouts in .NET MAUI
. You can control this spacing using the Spacing
, Padding
, and Margin
properties.
Spacing: This property is used in
StackLayout
and other layouts to set the space between individual child elements.Padding: This property adds space between the content of a layout and its boundary. You can set it on any layout or view to add inner space.
Margin: This property adds space outside the boundary of a layout or view. It is used to separate elements from each other and from the parent container.
Here’s an example demonstrating these properties:
<StackLayout Padding="20"
Spacing="10"
BackgroundColor="LightGray">
<Label Text="Label with Padding"
Padding="10, 20"
BackgroundColor="LightBlue"
TextColor="White" />
<Button Text="Button with Padding and Margin"
Padding="10, 5"
Margin="5, 0, 5, 0"
BackgroundColor="Green"
TextColor="White" />
<Entry Placeholder="Entry with Margin"
Margin="5, 0, 5, 0"
BackgroundColor="LightPink" />
</StackLayout>
- The
StackLayout
has a padding of 20, adding space around its contents. - The
Label
has both padding and background color, with padding creating space inside the label boundaries. - The
Button
andEntry
have margin properties that add space outside their boundaries, separating them from other elements.
In this example, padding and margin are used to create visual separation and hierarchy within the layout.
By understanding and utilizing these key aspects of StackLayout
and Grid
, you can effectively create a wide range of layouts in your .NET MAUI applications.