KEMBAR78
[Windows] Fixed StackLayout crashes on Windows with HeightRequest as 0 by Dhivya-SF4094 · Pull Request #29926 · dotnet/maui · GitHub
Skip to content

Conversation

Dhivya-SF4094
Copy link
Contributor

@Dhivya-SF4094 Dhivya-SF4094 commented Jun 11, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Issue Detail

Setting HeightRequest="0" with padding as some value on stack layouts throws exceptions ,when alignment is set in the opposite direction of the layout.

Root Cause

On Windows in .NET MAUI, setting HeightRequest="0" (or WidthRequest="0") with padding causes a layout exception. This happens because padding is subtracted from bounds.Height/bounds.Width which is explicitly set as 0, resulting in a negative size.

Description of Change

Updated ArrangeChildren methods in both VerticalStackLayoutManager and HorizontalStackLayoutManager to clamp the calculated available size using Math.Max. This ensures the available space is never negative, thereby preventing exceptions during layout on Windows while preserving expected behavior on all platforms.

Validated the behaviour in the following platforms

  • Android
  • Windows
  • iOS
  • Mac

Issues Fixed:

Fixes #29919

Screenshots

Before After

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Jun 11, 2025
@dotnet-policy-service
Copy link
Contributor

Hey there @@Dhivya-SF4094! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service dotnet-policy-service bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Jun 11, 2025
@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@Dhivya-SF4094 Dhivya-SF4094 marked this pull request as ready for review June 11, 2025 12:54
@Copilot Copilot AI review requested due to automatic review settings June 11, 2025 12:54
@Dhivya-SF4094 Dhivya-SF4094 requested a review from a team as a code owner June 11, 2025 12:54
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR prevents StackLayout crashes on Windows when HeightRequest or WidthRequest is set to 0 with padding and opposing alignment by clamping the available layout size to zero at minimum.

  • Added Math.Max(0, ...) clamps in both stack layout managers to avoid negative dimensions
  • Introduced a UI test and HostApp scenario to validate that the crash no longer occurs

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/Core/src/Layouts/VerticalStackLayoutManager.cs Clamped width calculation to non-negative
src/Core/src/Layouts/HorizontalStackLayoutManager.cs Clamped height calculation to non-negative
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29919.cs Added shared UI test waiting for the crash-prevention element
src/Controls/tests/TestCases.HostApp/Issues/Issue29919.cs Added HostApp page with test stacks and description label
Comments suppressed due to low confidence (2)

src/Core/src/Layouts/HorizontalStackLayoutManager.cs:53

  • [nitpick] The local variable height could be renamed to something more descriptive like availableHeight and declared as double for consistency with the vertical manager's width declaration.
var height = Math.Max(0, bounds.Height - padding.VerticalThickness);

src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29919.cs:19

  • Consider adding a more specific assertion or a follow-up check (e.g., verifying the layout bounds or that no exception was thrown) to make the test more robust and ensure the clamp logic behaves as expected.
App.WaitForElement("29919DescriptionLabel");

double stackHeight = padding.Top + bounds.Y;
double left = padding.Left + bounds.X;
double width = bounds.Width - padding.HorizontalThickness;
double width = Math.Max(0, bounds.Width - padding.HorizontalThickness);
Copy link

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The clamp logic (Math.Max(0, ...)) is duplicated in both layout managers. Consider extracting a helper method (e.g., ClampNonNegative) to improve code reuse and readability.

Suggested change
double width = Math.Max(0, bounds.Width - padding.HorizontalThickness);
double width = ClampNonNegative(bounds.Width - padding.HorizontalThickness);

Copilot uses AI. Check for mistakes.

@jsuarezruiz jsuarezruiz added area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter layout-stack labels Jun 12, 2025
@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

@bhavanesh2001 bhavanesh2001 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@PureWeen PureWeen changed the base branch from main to inflight/current July 3, 2025 17:14
@PureWeen PureWeen merged commit 83b36dc into dotnet:inflight/current Jul 3, 2025
78 checks passed
github-actions bot pushed a commit that referenced this pull request Jul 8, 2025
#29926)

* Fixed StackLayout crashes on Windows with HeightRequest 0, padding, and opposing alignment.

* Included test case for Vertical StackLayout

* Updated label content
github-actions bot pushed a commit that referenced this pull request Jul 9, 2025
#29926)

* Fixed StackLayout crashes on Windows with HeightRequest 0, padding, and opposing alignment.

* Included test case for Vertical StackLayout

* Updated label content
PureWeen pushed a commit that referenced this pull request Jul 10, 2025
#29926)

* Fixed StackLayout crashes on Windows with HeightRequest 0, padding, and opposing alignment.

* Included test case for Vertical StackLayout

* Updated label content
PureWeen pushed a commit that referenced this pull request Jul 15, 2025
#29926)

* Fixed StackLayout crashes on Windows with HeightRequest 0, padding, and opposing alignment.

* Included test case for Vertical StackLayout

* Updated label content
PureWeen pushed a commit that referenced this pull request Jul 17, 2025
#29926)

* Fixed StackLayout crashes on Windows with HeightRequest 0, padding, and opposing alignment.

* Included test case for Vertical StackLayout

* Updated label content
github-actions bot pushed a commit that referenced this pull request Jul 25, 2025
#29926)

* Fixed StackLayout crashes on Windows with HeightRequest 0, padding, and opposing alignment.

* Included test case for Vertical StackLayout

* Updated label content
github-actions bot pushed a commit that referenced this pull request Jul 31, 2025
#29926)

* Fixed StackLayout crashes on Windows with HeightRequest 0, padding, and opposing alignment.

* Included test case for Vertical StackLayout

* Updated label content
rmarinho pushed a commit that referenced this pull request Aug 1, 2025
* Added sample for GraphicsView

* Added the feature matrix code

* Added the test cases for the GraphicsView

* Updated the GraphicsViewFeaturetests

* Updated the GraphicsViewViewModel

* Addressed the feedbacks

* Added the snapshots for Mac and Windows

* Remove .NET 8 specific code for templates (#30342)

* [Catalyst] Fix Scrollbar does not align with FlowDirection change in ScrollView (#30241)

* fix added

* Revert "fix added"

This reverts commit 138797f.

* fix added

* fix updated

* [Android] Fix for OnSizeAllocated is not reported for Android AppShell Flyout content. (#30069)

* fix-22045 - Made changes on Android ShellFlyout sizing by adding OnDrawerOpened event and updating Content so OnSizeAllocated is called with correct values when drawer opens.

* fix-22045- Set the contentview frame value when the FlyoutView layout change.

* fix-22045- Changes committed.

* Permissions (BT & WiFi): don't add AccessFineLocation to required permissions if NeverForLocation flag is present (#20874)

* otherwise the permission-status will be 'denied' if AccessFineLocation is in the manifest, but denied at runtime
  (even if it is not strictly required)

* Fixed the RealParent Warning shown issue (#30156)

* Safe Get of RealParent to avoid warnings

* Optimized the fix

* Make it as default private

* Optimized the fix

* Modified the fix and added test

* Modified the fix based on concern and added test

* unwanted spacing

* Replaced the Parent property directly

* [iOS/MacCatalyst] Fix: Setting SelectedItem Programmatically and Then Immediately Setting ItemsSource to Null Causes a Crash (#29940)

* Re-evaluate indexpaths in PerformBatchUpdates

* Add UI test

* update test

* bring back null check

* add a check to see if ItemsSource is disposed

* [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

* [Windows] Fixed CanvasDrawingSession Exception on Clipping Image (#30028)

* [Windows] Fixed CanvasDrawingSession Exception

* Update in description label

* Updated Suggested Changes

* [Testing] Feature matrix UITest Cases for ScrollView Control (#30188)

* Added Feature Matrix for ScrollView

* Updated changes

* Changes updated

* Modified the test cases

* Modified UI

* Added snapshots

* Added images

* Modified Test Case

* Added images

* Resaved images

* [Testing] Feature Matrix UITest Cases for TimePicker Control  (#30271)

* modified UI and test cases

* updated test cases

* added new test cases

* updated test cases

* added android and iOS snapshots

* updated format

* updated flow direction code

* added mac and windows snapshots

* updated iOS snapshots

* [Catalyst] Adding and Removing ContextMenus currently does not work - fix (#30307)

* Disconnect context flyout handler

* Update MenuFlyoutHandler.iOS.cs

* Fix for TapGestureRecognizer ButtonMask always return 0 (#30372)

* Fix for TapGesture ButtonMask

* Added TestCase

* Update Testcase

* Modified Testcase

* fix extention typo (#30461)

* fix some incorrect casting using `as` (#30459)

if the type is known then a direct cast should be used instead of an as.
since, in the case where the assumption is wrong, then it is better to
get a cast exception instead of a null ref exception.

basically if code uses `as` and does not check for null when that
variable is used, then it is likely problematic code.

* [Testing] Feature Matrix UITest Cases for SearchBar Control  (#30398)

* added test cases

* added snapshots

* comment added

* snapshot added

* changed method name

* [Testing] Feature Matrix UITest Cases for DatePicker Control (#30159)

* added test cases

* added new test cases

* removed test cases

* updated test cases

* updated test cases

* changes in test case

* added new event

* updated test cases

* added culture test cases

* added snapshots

* added the failing condition

* modified test case

* added new test case code

* added mac and windows snapshots

* [create-pull-request] automated change (#30591)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
# Conflicts:
#	src/Compatibility/Core/src/Tizen/Forms.cs

* [Testing] Add UITest for Issue30147 on iOS (#30506)

* fix added

* Revert "fix added"

This reverts commit 138797f.

* test case added

* test sample modified

* test sample updated

* affected platform changed

* sample changes added

* [Android] Fix for Search Handler visual and functional bug in subtabs (#30467)

* fix-21119-Made changes to ShellToolbarTracker.cs to prevent search handler stacking on Android Shell tab navigation by removing existing menu items before adding new ones.

* fix-21119-Testcase and test snapshot added.

* fix-21119-Updated code changes. Also added Mac and Windows snapshots.

* [Windows] Fixed the Pasted Password Becomes Visible When IsPassword Is Enabled (#30353)

* Fixed the masked text issue of same value pasted into the textbox field

* Added the test for verifying the masked text

* [create-pull-request] automated change (#30700)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Fixed GraphicsView event handlers are triggered even when IsEnabled is set to False  (#30755)

* Fixed GraphicsView event handlers are triggered even when IsEnabled is set to False

* Updated PlatformTouchGraphicsView

* Updated test sample

* Fix duplicate app icon resizing (#30920)

Possibly helps with #30900

* Addressed the feedbacks

* Added sample for GraphicsView

* Added the feature matrix code

* Added the test cases for the GraphicsView

* Updated the GraphicsViewFeaturetests

* Updated the GraphicsViewViewModel

* Addressed the feedbacks

* Added the snapshots for Mac and Windows

* Addressed the feedbacks

---------

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
Co-authored-by: devanathan-vaithiyanathan <114395405+devanathan-vaithiyanathan@users.noreply.github.com>
Co-authored-by: BagavathiPerumal <93652794+BagavathiPerumal@users.noreply.github.com>
Co-authored-by: Janus Weil <janus@gcc.gnu.org>
Co-authored-by: Ahamed-Ali <102580874+Ahamed-Ali@users.noreply.github.com>
Co-authored-by: Bhavanesh N <bhavanesh001@gmail.com>
Co-authored-by: Dhivya-SF4094 <127717131+Dhivya-SF4094@users.noreply.github.com>
Co-authored-by: Subhiksha Chandrasekaran <subhiksha.c@syncfusion.com>
Co-authored-by: NafeelaNazhir <nafeela.nazhirhussain@syncfusion.com>
Co-authored-by: TamilarasanSF4853 <tamilarasan.velu@syncfusion.com>
Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
Co-authored-by: HarishwaranVijayakumar <harishwaran.vijayakumar@syncfusion.com>
Co-authored-by: Simon Cropp <simon.cropp@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
Co-authored-by: Jeremy Powell <47993233+jeremy-visionaid@users.noreply.github.com>
PureWeen pushed a commit that referenced this pull request Aug 1, 2025
#29926)

* Fixed StackLayout crashes on Windows with HeightRequest 0, padding, and opposing alignment.

* Included test case for Vertical StackLayout

* Updated label content
PureWeen pushed a commit that referenced this pull request Aug 1, 2025
#29926)

* Fixed StackLayout crashes on Windows with HeightRequest 0, padding, and opposing alignment.

* Included test case for Vertical StackLayout

* Updated label content
PureWeen added a commit that referenced this pull request Aug 1, 2025
* Added sample for GraphicsView

* Added the feature matrix code

* Added the test cases for the GraphicsView

* Updated the GraphicsViewFeaturetests

* Updated the GraphicsViewViewModel

* Addressed the feedbacks

* Added the snapshots for Mac and Windows

* Remove .NET 8 specific code for templates (#30342)

* [Catalyst] Fix Scrollbar does not align with FlowDirection change in ScrollView (#30241)

* fix added

* Revert "fix added"

This reverts commit 138797f.

* fix added

* fix updated

* [Android] Fix for OnSizeAllocated is not reported for Android AppShell Flyout content. (#30069)

* fix-22045 - Made changes on Android ShellFlyout sizing by adding OnDrawerOpened event and updating Content so OnSizeAllocated is called with correct values when drawer opens.

* fix-22045- Set the contentview frame value when the FlyoutView layout change.

* fix-22045- Changes committed.

* Permissions (BT & WiFi): don't add AccessFineLocation to required permissions if NeverForLocation flag is present (#20874)

* otherwise the permission-status will be 'denied' if AccessFineLocation is in the manifest, but denied at runtime
  (even if it is not strictly required)

* Fixed the RealParent Warning shown issue (#30156)

* Safe Get of RealParent to avoid warnings

* Optimized the fix

* Make it as default private

* Optimized the fix

* Modified the fix and added test

* Modified the fix based on concern and added test

* unwanted spacing

* Replaced the Parent property directly

* [iOS/MacCatalyst] Fix: Setting SelectedItem Programmatically and Then Immediately Setting ItemsSource to Null Causes a Crash (#29940)

* Re-evaluate indexpaths in PerformBatchUpdates

* Add UI test

* update test

* bring back null check

* add a check to see if ItemsSource is disposed

* [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

* [Windows] Fixed CanvasDrawingSession Exception on Clipping Image (#30028)

* [Windows] Fixed CanvasDrawingSession Exception

* Update in description label

* Updated Suggested Changes

* [Testing] Feature matrix UITest Cases for ScrollView Control (#30188)

* Added Feature Matrix for ScrollView

* Updated changes

* Changes updated

* Modified the test cases

* Modified UI

* Added snapshots

* Added images

* Modified Test Case

* Added images

* Resaved images

* [Testing] Feature Matrix UITest Cases for TimePicker Control  (#30271)

* modified UI and test cases

* updated test cases

* added new test cases

* updated test cases

* added android and iOS snapshots

* updated format

* updated flow direction code

* added mac and windows snapshots

* updated iOS snapshots

* [Catalyst] Adding and Removing ContextMenus currently does not work - fix (#30307)

* Disconnect context flyout handler

* Update MenuFlyoutHandler.iOS.cs

* Fix for TapGestureRecognizer ButtonMask always return 0 (#30372)

* Fix for TapGesture ButtonMask

* Added TestCase

* Update Testcase

* Modified Testcase

* fix extention typo (#30461)

* fix some incorrect casting using `as` (#30459)

if the type is known then a direct cast should be used instead of an as.
since, in the case where the assumption is wrong, then it is better to
get a cast exception instead of a null ref exception.

basically if code uses `as` and does not check for null when that
variable is used, then it is likely problematic code.

* [Testing] Feature Matrix UITest Cases for SearchBar Control  (#30398)

* added test cases

* added snapshots

* comment added

* snapshot added

* changed method name

* [Testing] Feature Matrix UITest Cases for DatePicker Control (#30159)

* added test cases

* added new test cases

* removed test cases

* updated test cases

* updated test cases

* changes in test case

* added new event

* updated test cases

* added culture test cases

* added snapshots

* added the failing condition

* modified test case

* added new test case code

* added mac and windows snapshots

* [create-pull-request] automated change (#30591)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
# Conflicts:
#	src/Compatibility/Core/src/Tizen/Forms.cs

* [Testing] Add UITest for Issue30147 on iOS (#30506)

* fix added

* Revert "fix added"

This reverts commit 138797f.

* test case added

* test sample modified

* test sample updated

* affected platform changed

* sample changes added

* [Android] Fix for Search Handler visual and functional bug in subtabs (#30467)

* fix-21119-Made changes to ShellToolbarTracker.cs to prevent search handler stacking on Android Shell tab navigation by removing existing menu items before adding new ones.

* fix-21119-Testcase and test snapshot added.

* fix-21119-Updated code changes. Also added Mac and Windows snapshots.

* [Windows] Fixed the Pasted Password Becomes Visible When IsPassword Is Enabled (#30353)

* Fixed the masked text issue of same value pasted into the textbox field

* Added the test for verifying the masked text

* [create-pull-request] automated change (#30700)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Fixed GraphicsView event handlers are triggered even when IsEnabled is set to False  (#30755)

* Fixed GraphicsView event handlers are triggered even when IsEnabled is set to False

* Updated PlatformTouchGraphicsView

* Updated test sample

* Fix duplicate app icon resizing (#30920)

Possibly helps with #30900

* Addressed the feedbacks

* Added sample for GraphicsView

* Added the feature matrix code

* Added the test cases for the GraphicsView

* Updated the GraphicsViewFeaturetests

* Updated the GraphicsViewViewModel

* Addressed the feedbacks

* Added the snapshots for Mac and Windows

* Addressed the feedbacks

---------

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
Co-authored-by: devanathan-vaithiyanathan <114395405+devanathan-vaithiyanathan@users.noreply.github.com>
Co-authored-by: BagavathiPerumal <93652794+BagavathiPerumal@users.noreply.github.com>
Co-authored-by: Janus Weil <janus@gcc.gnu.org>
Co-authored-by: Ahamed-Ali <102580874+Ahamed-Ali@users.noreply.github.com>
Co-authored-by: Bhavanesh N <bhavanesh001@gmail.com>
Co-authored-by: Dhivya-SF4094 <127717131+Dhivya-SF4094@users.noreply.github.com>
Co-authored-by: Subhiksha Chandrasekaran <subhiksha.c@syncfusion.com>
Co-authored-by: NafeelaNazhir <nafeela.nazhirhussain@syncfusion.com>
Co-authored-by: TamilarasanSF4853 <tamilarasan.velu@syncfusion.com>
Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
Co-authored-by: HarishwaranVijayakumar <harishwaran.vijayakumar@syncfusion.com>
Co-authored-by: Simon Cropp <simon.cropp@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
Co-authored-by: Jeremy Powell <47993233+jeremy-visionaid@users.noreply.github.com>
PureWeen pushed a commit that referenced this pull request Aug 1, 2025
#29926)

* Fixed StackLayout crashes on Windows with HeightRequest 0, padding, and opposing alignment.

* Included test case for Vertical StackLayout

* Updated label content
@github-actions github-actions bot locked and limited conversation to collaborators Aug 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter community ✨ Community Contribution layout-stack partner/syncfusion Issues / PR's with Syncfusion collaboration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StackLayout Throws Exception on Windows When Orientation Is Set with HeightRequest of 0, Padding, and Opposing Alignment

4 participants