| MAUI-Cross-Platform-Project | MAUI-Grid | |
📐 Layouts in .NET MAUI – StackLayout |
The StackLayout remains a foundational building block for creating user interfaces in .NET MAUI. It is designed to organize child views in a one-dimensional "stack," which can be arranged either vertically or horizontally. While it is a versatile tool for simple arrangements, modern MAUI development often favors more specialized, high-performance versions of this layout for standard use cases.
The primary role of a StackLayout is to take a collection of UI elements—such as buttons, labels, and images—and line them up without them overlapping.
HorizontalOptions="Center".
In the latest versions of .NET MAUI, Microsoft introduced VerticalStackLayout and HorizontalStackLayout as more performant alternatives.
The following example demonstrates a standard vertical layout for a user profile section using Spacing and HorizontalOptions.
<StackLayout Spacing="15"
Padding="20"
VerticalOptions="Center">
<Image Source="user_avatar.png"
HeightRequest="100"
WidthRequest="100"
HorizontalOptions="Center" />
<Label Text="Jane Doe"
FontSize="24"
FontAttributes="Bold"
HorizontalOptions="Center" />
<Label Text="Senior Software Engineer based in Seattle."
HorizontalOptions="Fill"
HorizontalTextAlignment="Center" />
<Button Text="Follow"
BackgroundColor="DeepSkyBlue"
TextColor="White"
WidthRequest="150"
HorizontalOptions="Center" />
</StackLayout>
The StackLayout remains the easiest way to prototype and build simple linear UIs. As applications grow in complexity, using VerticalStackLayout, HorizontalStackLayout, and switching to Grid when appropriate is key to maintaining high performance in cross-platform MAUI apps.
StackLayout is a layout container in .NET MAUI that arranges its child elements in a single line, either vertically or horizontally. It is one of the simplest and most commonly used layouts.
Vertical (default) or Horizontal.
<StackLayout Orientation="Vertical" Spacing="10">
<Label Text="Welcome to MAUI!" />
<Entry Placeholder="Enter your name" />
<Button Text="Submit" />
</StackLayout>
<StackLayout Orientation="Horizontal" Spacing="15">
<Label Text="Username:" />
<Entry Placeholder="Enter username" />
</StackLayout>
StackLayout is ideal when you need to arrange elements in a straight line.
For more complex layouts, other containers like Grid or FlexLayout may be better suited.
| MAUI-Cross-Platform-Project | MAUI-Grid | |