microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

Application crashed with unhandled exception "Layout cycle detected. Layout could not complete."

Open harvinders opened this issue 4 years ago • 5 comments

Describe the bug Not sure if I have enough information, but one of my applications crashed with the following exception. I thought it is something that the team would like to know. I have never seen such exception before.

My application has a ListView and the ListItems has few ItemsRepeater.

I would add more information if I see it happen again.

  Name Value Type
e {Windows.UI.Xaml.UnhandledExceptionEventArgs} Windows.UI.Xaml.UnhandledExceptionEventArgs
  ◢ Exception {"A cycle occurred while laying out the GUI."} System.Exception {Windows.UI.Xaml.LayoutCycleException}
  ▶ Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
  HResult -2144665580 int
  HelpLink null string
  IPForWatsonBuckets 0x00000000 System.UIntPtr
  ▶ InnerException null System.Exception
  IsTransient false bool
  Message "A cycle occurred while laying out the GUI." string
  RemoteStackTrace null string
  Source null string
  StackTrace null string
  TargetSite null System.Reflection.MethodBase
  WatsonBuckets null object
  _HResult -2144665580 int
  _className null string
  ▶ _data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
  _dynamicMethods null object
  _exceptionMethod null System.Reflection.MethodBase
  _helpURL null string
  ▶ _innerException null System.Exception
  _ipForWatsonBuckets 0x00000000 System.UIntPtr
  _message "A cycle occurred while laying out the GUI." string
  _remoteStackIndex 0 int
  _remoteStackTraceString null string
  _source null string
  _stackTrace null object
  _stackTraceString null string
  _watsonBuckets null object
  _xcode -532462766 int
  _xptrs 0x00000000 System.IntPtr
  ▶ Static members    

Version Info

NuGet package version: Microsoft.UI.Xaml 2.6.0-prerelease

Windows app type:

UWP Win32
Yes
Windows 10 version Saw the problem?
Insider Build (xxxxx)
May 2020 Update (19041)
November 2019 Update (18363) Yes
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop
Xbox
Surface Hub
IoT

Additional context

harvinders avatar Jan 20 '21 14:01 harvinders

@harvinders do you by any chance have a ListView or ItemsRepeater inside ListView wrapped into ScrollViewer?

beervoley avatar Jan 21 '21 06:01 beervoley

No @beervoley , I am not using ScrollViewer, as I kept the top most control as ListView following is my XAML files. The issue manifests when I scroll the ListView couple of times. Also, the application uses ReactiveUI for bindings in code behind, hence you would not see all the bindings in the XAML files.

MainPage.xaml

    <Grid x:Name="ContentArea" Margin="{StaticResource MediumLeftRightMargin}">
        <Grid Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}" RowSpacing="12">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="12"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <AutoSuggestBox Grid.Row="0" Width="300" x:Name="SearchBox" CornerRadius="8" HorizontalAlignment="Center" FontSize="{StaticResource XLargeFontSize}" QueryIcon="Find"/>
            <muxc:ProgressBar
                x:Name="ProgressBar"
                Grid.Row="1"
                Margin="{StaticResource SmallLeftRightMargin}"
                IsIndeterminate="True"
                VerticalAlignment="Center"
                HorizontalAlignment="Stretch" />
            <muxc:ItemsRepeater Grid.Row="2" x:Name="SuggestionList">
                <muxc:ItemsRepeater.Layout>
                    <muxc:StackLayout Orientation="Horizontal" Spacing="8"/>
                </muxc:ItemsRepeater.Layout>
                <DataTemplate x:DataType="system:String">
                    <HyperlinkButton Content="{x:Bind Mode=OneWay}" FontSize="{StaticResource XMediumFontSize}" />
                </DataTemplate>
            </muxc:ItemsRepeater>
            <ListView Grid.Row="3" x:Name="MeaningList" SelectionMode="None">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="core:Meaning">
                        <views:MeaningView HorizontalAlignment="Stretch" ViewModel="{x:Bind Mode=OneWay}" />
                    </DataTemplate>
                </ListView.ItemTemplate>
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    </Style>
                </ListView.ItemContainerStyle>

            </ListView>
        </Grid>
    </Grid>

MeaningView.xaml

<UserControl
    x:Class="Words.Views.MeaningView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Words.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controls="using:Microsoft.UI.Xaml.Controls"
    xmlns:core="using:Words.Core"
    xmlns:system="using:System"
    xmlns:controls1="using:Microsoft.Toolkit.Uwp.UI.Controls"
    mc:Ignorable="d">

    <Grid RowSpacing="8">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Spacing="16">
            <RichTextBlock Typography.StylisticSet4="True" FontFamily="Cambria">
                <Paragraph>
                    <Run x:Name="PartOfSpeech" Foreground="DarkRed" FontSize="{StaticResource LargeFontSize}"/>
                    <Run x:Name="Word" FontSize="{StaticResource XLargeFontSize}"/>
                </Paragraph>
            </RichTextBlock>
            <AppBarButton Icon="Add" VerticalAlignment="Bottom" Style="{StaticResource AppBarButtonStyle}" LabelPosition="Collapsed"/>
        </StackPanel>

        <controls:ItemsRepeater Grid.Row="1" x:Name="PronunciationList">
            <controls:ItemsRepeater.Layout>
                <controls:StackLayout Orientation="Horizontal" Spacing="8"/>
            </controls:ItemsRepeater.Layout>
            <DataTemplate x:DataType="core:Phonetic">
                <local:PronunciationView ViewModel="{x:Bind Mode=OneWay}"/>
            </DataTemplate>
        </controls:ItemsRepeater>

        <controls:ItemsRepeater Grid.Row="2" x:Name="DefinitionList">
            <DataTemplate x:DataType="core:Definition">
                <Grid RowSpacing="8" Margin="0 6">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="{x:Bind Sn}" FontSize="{StaticResource XMediumFontSize}"/>
                    <TextBlock Grid.Column="1" Text="{x:Bind DefinitionDefinition}" TextWrapping="WrapWholeWords" FontSize="{StaticResource XMediumFontSize}"/>
                    <TextBlock Grid.Column="1" Grid.Row="1" Text="{x:Bind Example}" TextWrapping="WrapWholeWords" FontSize="{StaticResource XMediumFontSize}" Foreground="{ThemeResource SystemControlHighlightAccentBrush}"/>
                    <controls:ItemsRepeater Grid.Column="1" Grid.Row="2" ItemsSource="{x:Bind Synonyms}" >
                        <controls:ItemsRepeater.Layout>
                            <controls:StackLayout Orientation="Horizontal" />
                        </controls:ItemsRepeater.Layout>
                        <DataTemplate x:DataType="system:String">
                            <HyperlinkButton Content="{x:Bind Mode=OneWay}" FontSize="{StaticResource XMediumFontSize}" />
                        </DataTemplate>
                    </controls:ItemsRepeater>
                </Grid>
            </DataTemplate>
        </controls:ItemsRepeater>
    </Grid>
</UserControl>

Also, after adding <controls:ItemsRepeater Grid.Column="1" Grid.Row="2" ItemsSource="{x:Bind Synonyms}" > the scrolling performance was also very bad.

harvinders avatar Jan 22 '21 01:01 harvinders

@beervoley Changing the layout to WrapLayout from windows community toolkit improved the performance and I am not seeing another crash, it may happen I might have to stress the app a little more.

<controls:ItemsRepeater.Layout>
   <uct:WrapLayout Orientation="Horizontal" />
</controls:ItemsRepeater.Layout>

harvinders avatar Jan 24 '21 02:01 harvinders

@harvinders that's interesting, I have never seen this issue before. I will try to have a look during the week :)

beervoley avatar Jan 24 '21 03:01 beervoley

I also have this problem on .NET MAUI, it always happens on windows on a specific page. I think there are some features windows doesn't support and weren't noticed by Microsoft when released.

99067718 avatar Sep 23 '22 09:09 99067718

I am experiencing this crash in .NET MAUI. The previous Xamarin.Forms version (which used UWP) worked fine. The MAUI version crashes with error "Layout cycle detected. Layout could not complete.". The crashing page contains a lazily-loaded view that has FlexLayout with a few items.

holecekp avatar Sep 28 '22 09:09 holecekp

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Jul 29 '23 15:07 github-actions[bot]

This issue happens consistently on Windows when moving a window between monitors with different Text Scales (one monitor is 100%, the other 150%). In my case it was caused by a Grid nested inside a Frame (although the actual issue may have been triggered by having too many height/width fields set to Auto or *; it's difficult to know). A simple layout that generates the issue is:

<ContentView ... >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Frame Grid.Row="0" CornerRadius="4">  <!-- Removing the wrapping frame resolves the "cycle" -->
            <Grid>
                 <!-- some controls .... -->
                 ...

jamesk-ctx avatar Aug 03 '23 18:08 jamesk-ctx

Strangely I started hitting this tonight.

Worked from the office today, everything was fine right up through my final commit/push. I came home, pulled, and hit this running under VS. My exception details look the same as OP's, except every now and the (1 out of 10) my exception message is instead "No such interface supported".

I'm on a WinUI 3 app using CommunityToolkit template and packages. Aside from that I can't really give much more info aside from a "me too". It's unsettling to get a null stack trace.

Abe-Froman avatar Oct 11 '23 03:10 Abe-Froman

The original repro descriptions in this issue should be fixed by a core layout change which was just completed for 1.5. Any layout issues which repro with the fix should be logged as new issues with specific repros to investigate.

codendone avatar Nov 16 '23 23:11 codendone