Skip to content

Instantly share code, notes, and snippets.

@wbokkers
Created November 24, 2021 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wbokkers/011011af9bf95066967bd4998dca9d77 to your computer and use it in GitHub Desktop.
Save wbokkers/011011af9bf95066967bd4998dca9d77 to your computer and use it in GitHub Desktop.
Immediate Crash on Loading ListView
<UserControl
x:Class="LayoutCycleTest.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:layoutcycletest="using:LayoutCycleTest"
mc:Ignorable="d"
>
<UserControl.Resources>
<Style x:Key="StretchedListViewItem" TargetType="ListViewItem">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="MinHeight" Value="4" />
<Setter Property="FocusVisualPrimaryThickness" Value="0" />
<Setter Property="FocusVisualSecondaryThickness" Value="0" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Margin" Value="0,0" />
<Setter Property="Padding" Value="0" />
</Style>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button Click="Reload">Reload</Button>
<TextBlock Margin="8,0,12,0" VerticalAlignment="Center" x:Name="tbSeed"/>
<TextBlock VerticalAlignment="Center" FontSize="11" Text="Scroll to end of the list using the TOUCH PAD to see Layout Cycle Detected Exception. If nothing happens, reload the view."/>
</StackPanel>
<ListView Grid.Row="1" Width="666.4" Height="343.2" BorderBrush="Black" BorderThickness="1" ItemContainerStyle="{StaticResource StretchedListViewItem}"
IsItemClickEnabled="False"
ScrollViewer.HorizontalScrollMode="Disabled"
ScrollViewer.IsDeferredScrollingEnabled="True"
x:Name="listView"
ScrollViewer.VerticalScrollMode="Auto"
SelectionMode="None">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel VerticalAlignment="Bottom" ItemsUpdatingScrollMode="KeepLastItemInView" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate x:DataType="layoutcycletest:TestModel">
<Border
x:Name="commItemRoot"
Margin="8,0,100,0"
HorizontalAlignment="Left"
BorderBrush="Gray"
BorderThickness="1,1,2,2"
CornerRadius="4">
<TextBlock
Padding="4"
VerticalAlignment="Top"
Margin="4,0,4,0"
HorizontalAlignment="Stretch"
Text="{x:Bind UiLabel, Mode=OneWay}"
IsTextSelectionEnabled="True"
TextWrapping="Wrap"
/>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</UserControl>
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
namespace LayoutCycleTest
{
/// <summary>
/// Random generator
/// </summary>
public static class TestRnd
{
public static Random Rnd { get; set; } = new Random();
}
public class TestModel
{
private static readonly string[] _str = new[]
{
"The Quick Brown Fox Jumps Over The Lazy Dog. The Quick Brown Fox Jumps Over The Lazy Dog. The Quick Brown Fox Jumps Over The Lazy Dog. The Quick Brown Fox Jumps Over The Lazy Dog.",
"Ab cde Fghi Jklm Nopq Rst Uvw Xyz. Ab cde Fghi Jklm Nopq Rst Uvw Xyz. Ab cde Fghi Jklm Nopq Rst Uvw Xyz. Ab cde Fghi Jklm Nopq Rst Uvw Xyz. Ab cde Fghi Jklm Nopq Rst Uvw Xyz. Ab cde Fghi Jklm Nopq Rst Uvw Xyz.",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"
};
public string UiLabel
{
get
{
var istr = TestRnd.Rnd.Next(2);
var str = _str[istr];
var startIndex = TestRnd.Rnd.Next(0, 11);
var maxIndex = TestRnd.Rnd.Next(20, str.Length - 1);
return str[startIndex..maxIndex];
}
}
}
public sealed partial class TestControl : UserControl
{
private int _seed = 9; // This seed crashes the app immediately.
private readonly ObservableCollection<TestModel> _testList = new();
public TestControl()
{
this.InitializeComponent();
Loaded += TestControl_Loaded;
}
private async void TestControl_Loaded(object sender, RoutedEventArgs e)
{
await Task.Delay(500); // Remove this delay to crash with yet another exception (maybe this is related)
LoadResults();
}
private void Reload(object sender, RoutedEventArgs e)
{
LoadResults();
}
private void LoadResults()
{
TestRnd.Rnd = new Random(_seed);
tbSeed.Text = _seed.ToString();
_seed++;
listView.Width = 615;
listView.Height = 365; // NOTE: There's some relation between some list view heights and crash behavior.
// Change to other height to 'solve' the crash.
_testList.Clear();
for (int i = 0; i < 50; ++i)
{
_testList.Add(new TestModel());
}
listView.ItemsSource = _testList;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment