Skip to content

LayoutCycleException caused by nested Borders in ControlTemplates #32406

@Oxymoron290

Description

@Oxymoron290

Description

We are encountering an intermittent, but highly reproducible LayoutCycleException on Windows when a MAUI view contains a sufficiently large visual tree with deeply nested Border elements inside control templates. The issue disappears entirely when standard MAUI Border controls are removed or replaced with a third-party border, suggesting a bug in Border layout logic or measure/arrange invalidation.

This issue has become our most frequent production crash.

The problem only appears when the UI is heavily populated. Example patterns triggering the crash could be a page containing 20+ data grids, each wrapped in a ControlTemplate containing Borders, any visual tree where a template injects multiple nested Borders several layers deep, or the exception emerges during layout/measure/arrange cycles without any custom layout logic

One work around that consistently avoid the crash involves hiding the view containing the border tree, wait about 10 seconds, and then show it again. If we wait less than 1 second, the layout cycle crash still occurs.

Expected Behavior

Border usage should not cause unrecoverable layout cycles

Actual Behavior

  • Unhandled LayoutCycleException thrown from Microsoft.UI.Xaml.WinUI.dll
  • When Application.Current.DebugSettings.LayoutCycleDebugBreakLevel = Low, the debugger breaks into native code with no managed stack available

Sample Exception

Microsoft.UI.Xaml.dll!LayoutCycleDetected
Exception: Layout cycle detected. Layout was invalidated during measure or arrange.

A small C# MAUI Project that triggers the exception is attached.

To reproduce the issue using the sample application, there are controls across the top of the app. the "Add Stuff" button will trigger the crash depending on the parameters to the right. The "Add no border stuff" will never trigger the crash, demonstrating the workaround described above. The sliders to the right allow you to configure the number of elements to add the level of depth to add them (It nests them inside horizontal stack layouts). There is also a button to clean up the elements.

The following table shows some sample data that I used while testing this MVP and what my results were. Each run was conducted on a new instance except for the last run.

Layers Elements Result
3 1024 Crashed
1 1024 No Crash
8 1 No Crash
8 25 No Crash
8 300 Crash
1 299 No Crash
2 304 No Crash
3 304 Crash
2 1024 Crash
1 1024 Crash (app was not reset from previous run)

Steps to Reproduce

On Windows in Visual Studio:

  1. File → New → Project → .NET MAUI App
  • Choose either “.NET MAUI App”
  • Leave defaults, name it anything (ex: BorderLayoutCycleRepro)
  • Target Windows
  1. Add a UI that creates a large number of nested controls
  • Open MainPage.xaml
  • Add HorizontalStackLayout with a border inside it, followed by another Horizontal Stack Layout, repeating. Do this many times. multiple layers >2 with hundreds of elements >300
  1. Run the app on Windows
  • In the running application, observe the LayoutCycleException crash. The application will exit.

Sample application avaialble here

Link to public reproduction project repository

https://github.com/FFIDX-Success/BorderTemplateCrashExample

Version with bug

9.0.111 SR11.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

11 25h2 26200.6899

Did you find any workaround?

One work around that consistently avoid the crash involves hiding the view containing the border tree, wait about 10 seconds, and then show it again. If we wait less than 1 second, the layout cycle crash still occurs.

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions