{"record":{"id":"c563c4dd96e07ff3","repo":"AvaloniaUI/Avalonia","slug":"namescope-is-completed-no-further-registrations-a","errorCode":null,"errorMessage":"NameScope is completed, no further registrations are allowed","messagePattern":"NameScope is completed, no further registrations are allowed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Controls/NameScope.cs","lineNumber":56,"sourceCode":"        }\n\n        /// <summary>\n        /// Sets the value of the attached <see cref=\"NameScopeProperty\"/> on a styled element.\n        /// </summary>\n        /// <param name=\"styled\">The styled element.</param>\n        /// <param name=\"value\">The value to set.</param>\n        public static void SetNameScope(StyledElement styled, INameScope? value)\n        {\n            _ = styled ?? throw new ArgumentNullException(nameof(styled));\n\n            styled.SetValue(NameScopeProperty, value);\n        }\n\n        /// <inheritdoc />\n        public void Register(string name, object element)\n        {\n            if (IsCompleted)\n                throw new InvalidOperationException(\"NameScope is completed, no further registrations are allowed\");\n\n            _ = name ?? throw new ArgumentNullException(nameof(name));\n            _ = element ?? throw new ArgumentNullException(nameof(element));\n\n            if (_inner.TryGetValue(name, out var existing))\n            {\n                if (existing != element)\n                {\n                    throw new ArgumentException($\"Control with the name '{name}' already registered.\");\n                }\n            }\n            else\n            {\n                _inner.Add(name, element);\n                if (_pendingSearches.Remove(name, out var tcs))\n                {\n                    tcs.SetResult(element);\n                }","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Controls/NameScope.cs#L38-L74","documentation":"Thrown by NameScope.Register when IsCompleted is true. A NameScope is 'completed' (sealed) after its owning template/control tree has been materialized — typically after ApplyTemplate finishes registering all named elements. Once sealed, no further name registrations are permitted because consumers (Find/Get) may already be resolving names against a frozen map.","triggerScenarios":"Calling nameScope.Register(name, element) on a NameScope whose IsCompleted flag has been set to true, which happens after the visual tree template is fully loaded and the scope is committed.","commonSituations":"Attempting to register a control into a NameScope after the template has been applied — e.g., dynamically adding a named child to a templated parent and trying to register it in the existing scope. Custom INameScope implementations that don't track completion correctly. Calling Register during a late-loaded content swap.","solutions":["Register named elements before the NameScope is completed — during template construction / initial child setup, not after ApplyTemplate.","If you need a new named element after load, create a fresh NameScope (or set a new NameScope on the element) rather than mutating the sealed one.","Check nameScope.IsCompleted before calling Register and route to a new scope when true."],"exampleFix":"// before\nscope.Register(\"myItem\", control); // throws if completed\n// after\nif (scope.IsCompleted)\n    NameScope.SetNameScope(host, new NameScope());\nNameScope.GetNameScope(host).Register(\"myItem\", control);","handlingStrategy":"validation","validationCode":"if (!nameScope.IsCompleted)\n    nameScope.Register(name, element);","typeGuard":null,"tryCatchPattern":"try { nameScope.Register(name, element); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"completed\"))\n{\n    // create/use a fresh NameScope instead\n}","preventionTips":["Register all named elements during template construction, before IsCompleted flips.","Check IsCompleted before late registrations.","Prefer defining names in XAML so registration happens automatically at the right time."],"tags":["namescope","template","registration","controls"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}