Skip to content

Commit a1c1807

Browse files
Dhivya-SF4094PureWeen
authored andcommitted
[Windows] Fixed StackLayout crashes on Windows with HeightRequest as 0 (#29926)
* Fixed StackLayout crashes on Windows with HeightRequest 0, padding, and opposing alignment. * Included test case for Vertical StackLayout * Updated label content
1 parent 6627119 commit a1c1807

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 29919, "StackLayout Throws Exception on Windows When Orientation Is Set with HeightRequest of 0, Padding, and Opposing Alignment", PlatformAffected.UWP)]
4+
public class Issue29919 : ContentPage
5+
{
6+
public Issue29919()
7+
{
8+
var stack = new StackLayout();
9+
var label = new Label
10+
{
11+
Text = "VerticalStackLayout and HorizontalStackLayout should not crash when WidthRequest or HeightRequest is explicitly set to 0, respectively.",
12+
AutomationId = "29919DescriptionLabel",
13+
};
14+
15+
var horizontalStack = new HorizontalStackLayout
16+
{
17+
Padding = new Thickness(5),
18+
HeightRequest = 0,
19+
VerticalOptions = LayoutOptions.Center
20+
};
21+
22+
var verticalStack = new VerticalStackLayout
23+
{
24+
Padding = new Thickness(5),
25+
WidthRequest = 0,
26+
HeightRequest = 100,
27+
HorizontalOptions = LayoutOptions.Center
28+
};
29+
30+
stack.Children.Add(label);
31+
stack.Children.Add(horizontalStack);
32+
stack.Children.Add(verticalStack);
33+
Content = stack;
34+
}
35+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue29919 : _IssuesUITest
8+
{
9+
public Issue29919(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
13+
public override string Issue => "StackLayout Throws Exception on Windows When Orientation Is Set with HeightRequest of 0, Padding, and Opposing Alignment";
14+
15+
[Test]
16+
[Category(UITestCategories.Layout)]
17+
public void StackLayoutWindowsCrashWithZeroHeight()
18+
{
19+
App.WaitForElement("29919DescriptionLabel");
20+
}
21+
}

src/Core/src/Layouts/HorizontalStackLayoutManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public override Size ArrangeChildren(Rect bounds)
5050

5151
double top = padding.Top + bounds.Top;
5252

53-
var height = bounds.Height - padding.VerticalThickness;
53+
var height = Math.Max(0, bounds.Height - padding.VerticalThickness);
5454

5555
// Figure out where we're starting from
5656
double xPosition = padding.Left + bounds.Left;

src/Core/src/Layouts/VerticalStackLayoutManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public override Size ArrangeChildren(Rect bounds)
4949

5050
double stackHeight = padding.Top + bounds.Y;
5151
double left = padding.Left + bounds.X;
52-
double width = bounds.Width - padding.HorizontalThickness;
52+
double width = Math.Max(0, bounds.Width - padding.HorizontalThickness);
5353

5454
for (int n = 0; n < Stack.Count; n++)
5555
{

0 commit comments

Comments
 (0)