admin管理员组

文章数量:1122832

As far as I've worked with MAUI, I have only ever been using <VerticalStackLayout/> and <HorizontalStackLayout/> to position child elements horizontally or vertically.

I rarely or pretty much never use <StackLayout/> as I read it might perform slightly worse.

When, if at all, is it recommended to use <StackLayout/> and adjust its Orientation attribute?

Should I use...

<StackLayout Orientation="Horizontal">
<StackLayout/>

<StackLayout Orientation="Vertical">
<StackLayout/>

...instead of...

<HorizontalStackLayout>
<HorizontalStackLayout/>

<VerticalStackLayout>
<VerticalStackLayout/>

...or the other way around, depending on different scenarios?

As far as I've worked with MAUI, I have only ever been using <VerticalStackLayout/> and <HorizontalStackLayout/> to position child elements horizontally or vertically.

I rarely or pretty much never use <StackLayout/> as I read it might perform slightly worse.

When, if at all, is it recommended to use <StackLayout/> and adjust its Orientation attribute?

Should I use...

<StackLayout Orientation="Horizontal">
<StackLayout/>

<StackLayout Orientation="Vertical">
<StackLayout/>

...instead of...

<HorizontalStackLayout>
<HorizontalStackLayout/>

<VerticalStackLayout>
<VerticalStackLayout/>

...or the other way around, depending on different scenarios?

Share Improve this question edited Nov 21, 2024 at 15:22 Al John asked Nov 21, 2024 at 15:05 Al JohnAl John 8708 silver badges36 bronze badges 2
  • 1 I think StackLayout is just a facade left in place for backward compatibility. I don't think there is any performance difference, although for future proofing its probably better to stop using it – Jason Commented Nov 21, 2024 at 15:18
  • Thanks, I guess I'll keep using the other layout types if it's just a trace then. – Al John Commented Nov 21, 2024 at 15:31
Add a comment  | 

1 Answer 1

Reset to default 1

From a GitHub issue on MAUI (https://github.com/dotnet/maui/issues/6766) there is a simple explanation for performance on how the StackLayout will work compared to the HorizontalStackLayout and VerticalStackLayout.

The simplest way to look at it would be to stick to this:

  1. If you know the orientation of the layout will not change then use HorizontalStackLayout or VerticalStackLayout depending on the orientation you need.

  2. If you need to change the orientation and it might change at runtime then use StackLayout E.G. <StackLayout Orientation="{Binding MyStackOrientation}">

Since Xamarin Forms only had StackLayout, porting apps to MAUI is relatively easy as the code does not have to change, so also helps for backward compatibility - developers don't have to go around all their XAML updating their stack layouts.

The official docs also mention StackLayout being less performant though there is no explanation. https://learn.microsoft.com/en-us/dotnet/maui/user-interface/layouts/horizontalstacklayout?view=net-maui-8.0

本文标签: performanceWhich StackLayout type should I use in MAUIStack Overflow