AvaloniaUI/Avalonia · error · InvalidOperationException

The control doesn't have an associated name scope, probably

Error message

The control doesn't have an associated name scope, probably no registrations has been done yet

What it means

Thrown by NameScopeExtensions.Get<T>(this ILogical anchor, string name) when the anchor control and its logical ancestors have no associated INameScope. The method walks up the logical tree looking for a NameScope and, finding none, treats the lookup as impossible.

Source

Thrown at src/Avalonia.Base/Controls/NameScopeExtensions.cs:107

        }

        /// <summary>
        /// Gets a named element from an <see cref="INameScope"/> or throws if no element of the
        /// requested name was found.
        /// </summary>
        /// <typeparam name="T">The element type.</typeparam>
        /// <param name="anchor">The control to take the name scope from.</param>
        /// <param name="name">The name.</param>
        /// <returns>The named element.</returns>
        public static T Get<T>(this ILogical anchor, string name)
            where T : class
        {
            _ = anchor ?? throw new ArgumentNullException(nameof(anchor));
            _ = name ?? throw new ArgumentNullException(nameof(name));

            var nameScope = (anchor as INameScope) ?? NameScope.GetNameScope((StyledElement)anchor);
            if (nameScope == null)
                throw new InvalidOperationException(
                    "The control doesn't have an associated name scope, probably no registrations has been done yet");
            
            return nameScope.Get<T>(name);
        }
        
        public static INameScope? FindNameScope(this ILogical control)
        {
            _ = control ?? throw new ArgumentNullException(nameof(control));

            var scope = control.GetSelfAndLogicalAncestors()
                .OfType<StyledElement>()
                .Select(x => (x as INameScope) ?? NameScope.GetNameScope(x))
                .FirstOrDefault(x => x != null);
            return scope;
        }
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Wait until the control is attached to the visual tree (e.g., in OnAttachedToVisualTree or after Loaded) before calling Get.
  2. Use an anchor that is inside the templated tree where the names are registered.
  3. If you have a direct reference to the NameScope, call nameScope.Get<T>(name) instead of the anchor-based overload.

Example fix

// before (in constructor)
var btn = this.Get<Button>("saveBtn"); // no namescope yet
// after (after attach)
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
    var btn = this.Get<Button>("saveBtn");
}
Defensive patterns

Strategy: validation

Validate before calling

var scope = (anchor as INameScope) ?? NameScope.GetNameScope(anchor as StyledElement);
if (scope is null)
    throw new InvalidOperationException("No NameScope; ensure control is attached to the visual tree.");
return scope.Get<T>(name);

Try / catch

try { return anchor.Get<T>(name); }
catch (InvalidOperationException ex) when (ex.Message.Contains("name scope"))
{ /* defer to OnAttachedToVisualTree and retry */ return null; }

Prevention

When it happens

Trigger: Calling control.Get<T>("name") on a control that is not inside any templated/named tree — e.g., a detached control not yet added to the visual tree, or a control whose ancestors never had a NameScope attached.

Common situations: Calling Get/Find on a control in a constructor or Loaded handler before it is attached to the visual/logical tree. Using a plain control (no template) as the anchor where no NameScope was ever set. Querying a control from a different visual tree branch than where the names live.

Related errors


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