AvaloniaUI/Avalonia · error · InvalidOperationException

Cannot determine visual parent.

Error message

Cannot determine visual parent.

What it means

Thrown by PageSlide.GetVisualParent when the resolved parent p1 is null, meaning neither 'from' nor 'to' has a VisualParent. The transition cannot position or animate controls that are detached from the visual tree.

Source

Thrown at src/Avalonia.Base/Animation/PageSlide.cs:236

        /// <param name="to">The to control.</param>
        /// <returns>The common parent.</returns>
        /// <exception cref="ArgumentException">
        /// The two controls do not share a common parent.
        /// </exception>
        /// <remarks>
        /// Any one of the parameters may be null, but not both.
        /// </remarks>
        protected static Visual GetVisualParent(Visual? from, Visual? to)
        {
            var p1 = (from ?? to)!.VisualParent;
            var p2 = (to ?? from)!.VisualParent;

            if (p1 != null && p2 != null && p1 != p2)
            {
                throw new ArgumentException("Controls for PageSlide must have same parent.");
            }

            return p1 ?? throw new InvalidOperationException("Cannot determine visual parent.");
        }
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Add at least one of the controls to a parent panel before starting the slide.
  2. Defer the transition until both controls report a non-null Parent/VisualParent.
  3. If a control was removed, re-parent it or pass it as the null argument.

Example fix

// before
var slide = new PageSlide();
slide.Start(from: detachedA, to: detachedB); // both unparented

// after
panel.Children.Add(detachedA);
panel.Children.Add(detachedB);
slide.Start(from: detachedA, to: detachedB);
Defensive patterns

Strategy: validation

Validate before calling

static bool HasAnyParent(Visual? a, Visual? b) =>
    (a ?? b)?.VisualParent is not null;

Try / catch

if (!HasAnyParent(from, to)) { panel.Children.Add(to); }
slide.Start(from, to);

Prevention

When it happens

Trigger: Calling PageSlide with two controls that are both unparented (not yet added to any visual); calling it after both controls were removed from the tree; passing two nulls (GetVisualParent requires at least one non-null).

Common situations: Invoking a page transition in a constructor or handler before the controls are attached; animating freshly-created controls that were never added to a panel.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/f370a7c353ec77c3. Report an issue: GitHub.