Previous MAUI-FlexLayout MAUI Introduction Next

📐 Layouts in .NET MAUI – FlexLayout

.NET MAUI Layout Architecture

In the architecture of .NET MAUI (Multi-platform App UI), layouts are the fundamental containers used to compose user interface controls into visual structures. While the framework provides a wide variety of specialized tools, there are eight distinct layout options—six primary built-in classes, a bindable mechanism, and a custom option—that developers use to build responsive cross-platform applications.

Each of these layouts serves a unique purpose, ranging from simple linear stacks to complex, coordinate-based positioning.

1. StackLayout

The StackLayout is the most basic tool for organizing child elements in a one-dimensional line.

  • Behavior: It can arrange items either vertically or horizontally using the Orientation property.
  • Best For: Simple, linear groupings of controls like a form with a label followed by an entry.
  • Performance Note: While flexible, it is often less performant than its specialized counterparts (Vertical/Horizontal) because it must calculate dimensions for both possible orientations.

2. VerticalStackLayout

A highly optimized version of the stack layout designed specifically for vertical orientation.

  • Behavior: It positions elements one after another in a single column.
  • Best For: Creating vertical lists or page flows where you know items will always be stacked top-to-bottom.

3. HorizontalStackLayout

The horizontal counterpart to the vertical stack, optimized for performance.

  • Behavior: It aligns child elements side-by-side in a single row.
  • Best For: Toolbars, button groups, or horizontal headers.

4. Grid

The Grid is widely considered the most powerful and frequently used layout in .NET MAUI.

  • Behavior: It organizes elements into a 2D structure of rows and columns.
  • Flexibility: You can define row heights and column widths as absolute (pixels), Auto (size to content), or * (proportional star sizing).
  • Best For: Complex interfaces like calculators, dashboard views, or multi-column forms.

5. FlexLayout

Based on the popular web CSS Flexbox module, FlexLayout offers advanced fluidity.

  • Behavior: Like a stack layout, it arranges items linearly, but it can wrap items to a new row or column if space runs out.
  • Best For: Responsive designs, such as a photo gallery or a tag cloud where the number of items per row should change based on screen width.

6. AbsoluteLayout

This layout provides the highest level of control by allowing you to specify exact positions.

  • Behavior: You position child elements using absolute coordinates or proportional values relative to the layout's size.
  • Best For: Overlays, custom floating buttons, or decorative elements that need to overlap other parts of the UI.

7. BindableLayout

BindableLayout is technically a set of attached properties that allows any layout class (like a StackLayout or Grid) to generate its children from a data source.

  • Behavior: Instead of manually defining children in XAML, you bind to a collection of objects, and the layout creates a view for each item using a template.
  • Best For: Repeating UI elements where the data is dynamic but does not require the advanced features of a CollectionView.

8. Custom Layouts

For designs that cannot be achieved with the built-in tools, .NET MAUI allows you to build your own layout logic.

  • Behavior: By subclassing the base Layout class and providing a custom ILayoutManager, you can define exactly how children are measured and positioned.
  • Best For: Unique UI patterns like a circular menu, a cascading stack, or a specialized Z-stack.

Summary of Differences

  • Speed/Simple: Use VerticalStackLayout or HorizontalStackLayout.
  • Structured/Complex: Use Grid.
  • Adaptive/Fluid: Use FlexLayout.
  • Dynamic Data: Use BindableLayout.
  • Specific Precision: Use AbsoluteLayout.

In the modern development landscape, most professional apps use a combination of Grids for the overall page structure and FlexLayouts or StackLayouts for smaller internal components to ensure the app looks great on both mobile phones and desktop monitors. If you are building a highly interactive app, explore the .NET MAUI Community Toolkit for additional layout options like UniformItemsLayout.

📐 .NET MAUI Layouts Comparison

Layout Description Best Use Case
🧩 StackLayout Arranges elements in a single line (vertical or horizontal). Simple forms, menus, linear content.
➡️ HorizontalStackLayout Optimized stack for horizontal arrangement. Toolbars, horizontal lists.
⬇️ VerticalStackLayout Optimized stack for vertical arrangement. Forms, vertical lists.
🔲 Grid Organizes content into rows and columns. Dashboards, structured forms, tabular layouts.
🔧 FlexLayout Flexible arrangement similar to CSS Flexbox, supports wrapping and alignment. Responsive layouts, adaptive toolbars, wrapping content.
📍 AbsoluteLayout Positions elements at exact coordinates or relative bounds. Overlays, custom positioning, layered UI.
📐 RelativeLayout Positions elements relative to other elements. Dynamic layouts where controls depend on each other.
📦 ContentView Simple container for a single child view. Reusable UI components, custom controls.

Conclusion

.NET MAUI provides 8 main layouts. Use StackLayout for simple linear designs, Grid for structured layouts, FlexLayout for responsive designs, and Absolute/RelativeLayout for precise positioning. ContentView is best for reusable components.

Back to Index
Previous MAUI-FlexLayout MAUI Introduction Next
*