Is it recommended to use "StackLayout" in MAUI?

Kevin Anchapaxi 40 Reputation points
2025-02-11T14:00:23.9333333+00:00

I understand that in MAUI there are objects like VerticalStackLayout, which are better optimized, but when creating slightly more complex objects, I have found it better to continue using the classic StackLayout since it processes the properties "....AndExpand". However, I am unsure if this is the best practice in MAUI or if Microsoft plans to remove StackLayout in the future.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,378 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,925 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 48,421 Reputation points Microsoft Vendor
    2025-02-12T01:08:07.2333333+00:00

    Hello,

    For situations where ...AndExpand is needed, the best practice in Maui is not to use StackLayout but to use Grid for layout. For changes in Xamarin.Forms and Maui regarding StackLayout, you can refer to this document for more information.

    1. If your layout is anything other than a StackLayout, remove all uses of AndExpand. Just as in Xamarin.Forms, in .NET MAUI the AndExpand layout options have no effect on any layout other than StackLayout.
    2. Remove any AndExpand properties which are orthogonal to the stacking direction. For example, if you have a StackLayout with an Orientation of Vertical, and it has a child with a HorizontalAligment="CenterAndExpand" - that layout options has no effect and can be removed.
    3. If you have any remaining AndExpand properties on a StackLayout, you should convert that StackLayout to a Grid. A Grid is designed to subdivide a space, and will provide the layout that AndExpand provided in Xamarin.Forms. The following example shows a Xamarin.Forms StackLayout that uses an AndExpand property:
    <StackLayout>
        <Label Text="Hello world!"/>
        <Image VerticalOptions="FillAndExpand" Source="dotnetbot.png"/>
    </StackLayout>
    

    This can be converted to a Grid in .NET MAUI:

    <Grid RowDefinitions="Auto, *">
        <Label Text="Hello world!"/>
        <Image Grid.Row="1" Source="dotnetbot.png"/>
    </Grid>
    

    When performing this conversion, anything that was marked AndExpand in the StackLayout should go in its own row or column with a size of * in the Grid.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.