From 0aac1e4e0406340d239677f0ef5174dd78b99808 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Wed, 18 Sep 2024 01:03:13 -0700 Subject: [PATCH] Validate visual parent on attaching to the tree (#17049) * Validate visual parent on attaching to the tree * Update Visual.cs --- 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 55b0260eaf..cf9a050185 100644 --- a/src/Avalonia.Base/Visual.cs +++ b/src/Avalonia.Base/Visual.cs @@ -493,6 +493,10 @@ namespace Avalonia 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 @@ namespace Avalonia 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;