From 1f2c6231929737ecd7c7a745c3d3a5e98e1b505c Mon Sep 17 00:00:00 2001 From: Max Katz Date: Tue, 17 Sep 2024 18:11:07 -0700 Subject: [PATCH 1/2] Validate visual parent on attaching to the tree --- src/Avalonia.Base/Visual.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Base/Visual.cs b/src/Avalonia.Base/Visual.cs index 55b0260eaf1..25da298d025 100644 --- a/src/Avalonia.Base/Visual.cs +++ b/src/Avalonia.Base/Visual.cs @@ -493,6 +493,10 @@ protected virtual void OnAttachedToVisualTreeCore(VisualTreeAttachmentEventArgs Logger.TryGet(LogEventLevel.Verbose, LogArea.Visual)?.Log(this, "Attached to visual tree"); _visualRoot = e.Root; + if (_visualParent is null) + { + throw new InvalidOperationException("Visual was attached to the root without being added to the visual parent first.") + } if (RenderTransform is IMutableTransform mutableTransform) { @@ -505,14 +509,14 @@ protected virtual void OnAttachedToVisualTreeCore(VisualTreeAttachmentEventArgs AttachToCompositor(compositingRenderer.Compositor); } InvalidateMirrorTransform(); - UpdateIsEffectivelyVisible(_visualParent!.IsEffectivelyVisible); + UpdateIsEffectivelyVisible(_visualParent.IsEffectivelyVisible); OnAttachedToVisualTree(e); AttachedToVisualTree?.Invoke(this, e); InvalidateVisual(); - _visualRoot.Renderer.RecalculateChildren(_visualParent!); + _visualRoot.Renderer.RecalculateChildren(_visualParent); - if (ZIndex != 0 && _visualParent is { }) + if (ZIndex != 0) _visualParent.HasNonUniformZIndexChildren = true; var visualChildren = VisualChildren; From b06ab0a9f2ae07d486b524509ff840715c12e5ad Mon Sep 17 00:00:00 2001 From: Max Katz Date: Tue, 17 Sep 2024 18:17:02 -0700 Subject: [PATCH 2/2] Update Visual.cs --- src/Avalonia.Base/Visual.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Base/Visual.cs b/src/Avalonia.Base/Visual.cs index 25da298d025..cf9a050185d 100644 --- a/src/Avalonia.Base/Visual.cs +++ b/src/Avalonia.Base/Visual.cs @@ -495,7 +495,7 @@ protected virtual void OnAttachedToVisualTreeCore(VisualTreeAttachmentEventArgs _visualRoot = e.Root; if (_visualParent is null) { - throw new InvalidOperationException("Visual was attached to the root without being added to the visual parent first.") + throw new InvalidOperationException("Visual was attached to the root without being added to the visual parent first."); } if (RenderTransform is IMutableTransform mutableTransform)