0

Windows Phone C#: “Layout cycle detected. Layout could not complete.” on LongListSelector Fix

Came across a very weird problem today that has occurred on my application Steam Live which I’ve never come across before…

On a certain Steam Profile I’d get a crash with the following message: “Layout cycle detected.  Layout could not complete.”

After a lot of searching, confusion and everything else I’ve managed to find a fix which I have no idea why it works.

Fix:

Changing the font size of one of my TextBlocks from 21 to any other value.

Finding the problem:

I’ve had a look around at it appears a lot of other people have had problems with the LongListSelector. In future I will be avoiding it as it is clearly buggy and has weird things going on.

My method for finding the problem was to incrementally remove XAML controls from my LongListSelector until it started working. Once I removed the right one it was a case of seeing what was different about it from the others.

Entire XAML:

<phone:LongListSelector x:Name="OwnedGameListBox" Tap="OwnedGameListBoxTap" ScrollViewer.VerticalScrollBarVisibility="Disabled" ItemRealized="OwnedGameListBox_ItemRealized" ItemUnrealized="OwnedGameListBox_ItemUnrealized" BorderThickness="0,20,0,0"  >
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical" Tap="OwnedGameListBoxTap" Margin="0,0,0,12">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Tap="StackPanel_Tap_1">
                    <Image Width="60" Source="{Binding getSmallImageActualURL}" Height="60"  Margin="3" VerticalAlignment="Top" />
                    <StackPanel Margin="15,0,0,0">
                        <TextBlock Width="320" TextWrapping="Wrap" Text="{Binding name}" Margin="0,0,0,0" FontSize="32" />
                        <TextBlock Text="{Binding getTotalPlaytimeFormatted}" Margin="0,0,0,0" TextWrapping="Wrap" FontSize="21" >
                            <TextBlock.Foreground>
                                <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/>
                            </TextBlock.Foreground>
                        </TextBlock>
                    </StackPanel>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

Fixed:

<TextBlock Text="{Binding getTotalPlaytimeFormatted}" Margin="0,0,0,0" TextWrapping="Wrap" FontSize="20" >