From 4a89ed4bd6ab04f7478b098bd5ae54bf2fe7957b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 16 Jul 2021 13:04:01 +0200 Subject: [PATCH] Repro of sizing issue reported by customer. --- samples/Sandbox/MainWindow.axaml | 1 + samples/Sandbox/MainWindow.axaml.cs | 81 +++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/samples/Sandbox/MainWindow.axaml b/samples/Sandbox/MainWindow.axaml index 6929f192c7..35cfd9aef8 100644 --- a/samples/Sandbox/MainWindow.axaml +++ b/samples/Sandbox/MainWindow.axaml @@ -1,4 +1,5 @@ + diff --git a/samples/Sandbox/MainWindow.axaml.cs b/samples/Sandbox/MainWindow.axaml.cs index 3d54036d29..e6ebd21e5c 100644 --- a/samples/Sandbox/MainWindow.axaml.cs +++ b/samples/Sandbox/MainWindow.axaml.cs @@ -1,8 +1,16 @@ +using System; +using System.Threading.Tasks; using Avalonia; using Avalonia.Controls; +using Avalonia.Interactivity; +using Avalonia.Layout; using Avalonia.Markup.Xaml; +using Avalonia.Media; +using Avalonia.Threading; using Avalonia.Win32.WinRT.Composition; +#nullable enable + namespace Sandbox { public class MainWindow : Window @@ -17,5 +25,78 @@ namespace Sandbox { AvaloniaXamlLoader.Load(this); } + + private void Button_OnClick(object? sender, RoutedEventArgs e) + { + var i = 20; + + void Go() + { + var window = ShowWindow(); + window.Opened += async delegate (object? o, EventArgs args) + { + await Task.Delay(2000); + window.Close(); + if (i-- > 0) + { + Go(); + } + }; + } + + Go(); + } + + private Window ShowWindow() + { + var window = new Window(); + var heights = new[] { 491, 494, 554 }; + var stack = new StackPanel(); + stack.Orientation = Orientation.Horizontal; + + for (var i = 0; i < heights.Length; i++) + { + var border = new Border(); + border.Background = new SolidColorBrush(Color.FromRgb(200, 200, 200)); + border.Width = 100; + border.Child = new TextBlock() { Text = i.ToString() }; + border.Height = heights[i] - 5; + border.VerticalAlignment = VerticalAlignment.Top; + stack.Children.Add(border); + } + + stack.Height = 1; + window.Content = stack; + window.VerticalContentAlignment = VerticalAlignment.Top; + window.SizeToContent = SizeToContent.Height; + window.CanResize = false; + window.WindowStartupLocation = WindowStartupLocation.CenterOwner; + + void SetHeight(int i) + { + Dispatcher.UIThread.InvokeAsync(() => + { + stack.Height = heights[i]; + }); + } + + async Task WhenLayoutReady() + { + await Task.Delay(176); + SetHeight(0); + } + + WhenLayoutReady().ContinueWith(async _ => { + Task.Run(async () => + { + await Task.Delay(22); + SetHeight(1); + await Task.Delay(20); + SetHeight(2); + }); + Dispatcher.UIThread.InvokeAsync(() => { window.Show(); }); + }, TaskContinuationOptions.ExecuteSynchronously); + return window; + } } }