From 3848dab2fe195ada5a9e5e4b652b2ee6ff5d3c4e Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Tue, 15 Feb 2022 17:20:47 +0000 Subject: [PATCH] dont start animation until we have attached to visual tree. --- .../TransitioningContentControl.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/TransitioningContentControl.cs b/src/Avalonia.Controls/TransitioningContentControl.cs index 9562805416..8b702d91eb 100644 --- a/src/Avalonia.Controls/TransitioningContentControl.cs +++ b/src/Avalonia.Controls/TransitioningContentControl.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using Avalonia.Animation; +using Avalonia.Threading; namespace Avalonia.Controls; @@ -18,7 +19,7 @@ public class TransitioningContentControl : ContentControl /// public static readonly StyledProperty PageTransitionProperty = AvaloniaProperty.Register(nameof(PageTransition), - new CrossFade(TimeSpan.FromSeconds(0.5))); + new CrossFade(TimeSpan.FromSeconds(0.125))); /// @@ -46,13 +47,20 @@ public class TransitioningContentControl : ContentControl private set => SetAndRaise(DisplayedContentProperty, ref _displayedContent, value); } + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); + + Dispatcher.UIThread.Post(() => UpdateContentWithTransition(Content)); + } + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { base.OnPropertyChanged(change); if (change.Property == ContentProperty) { - UpdateContentWithTransition(Content); + Dispatcher.UIThread.Post(() => UpdateContentWithTransition(Content)); } } @@ -62,6 +70,11 @@ public class TransitioningContentControl : ContentControl /// New content to set. private async void UpdateContentWithTransition(object? content) { + if (VisualRoot is null) + { + return; + } + _lastTransitionCts?.Cancel(); _lastTransitionCts = new CancellationTokenSource();