{"record":{"id":"760e90a0a2c5ce84","repo":"AvaloniaUI/Avalonia","slug":"control-with-the-name-name-already-registered","errorCode":null,"errorMessage":"Control with the name '{name}' already registered.","messagePattern":"Control with the name '(.+?)' already registered\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Controls/NameScope.cs","lineNumber":65,"sourceCode":"            _ = 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                }\n            }\n        }\n\n        public SynchronousCompletionAsyncResult<object?> FindAsync(string name)\n        {\n            var found = Find(name);\n            if (found != null)\n                return new SynchronousCompletionAsyncResult<object?>(found);\n            if (IsCompleted)","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Controls/NameScope.cs#L47-L83","documentation":"Thrown by NameScope.Register when a different element is already registered under the same name in the same NameScope. Registering the identical element reference twice is allowed (idempotent), but registering two distinct objects under one name violates XAML name uniqueness and is rejected.","triggerScenarios":"Calling nameScope.Register(\"myName\", elementB) when nameScope already contains elementA != elementB under \"myName\". Common with two controls sharing x:Name in the same template, or programmatic registration of a duplicate key.","commonSituations":"Two controls in the same XAML file/template share the same x:Name. Programmatic creation adds a named control whose name collides with a template-defined peer. Copy/paste of XAML without renaming. Shared DataTemplate reused in a context that double-registers.","solutions":["Ensure every x:Name in a single NameScope (template/file) is unique.","Before programmatic Register, check scope.Find(name) and either skip or remove the old entry if appropriate.","If the collision is intentional (replacement), use a new NameScope rather than reusing the populated one."],"exampleFix":"// before\nscope.Register(\"item\", controlA);\nscope.Register(\"item\", controlB); // throws\n// after\nif (scope.Find(\"item\") is null)\n    scope.Register(\"item\", controlB);","handlingStrategy":"validation","validationCode":"if (nameScope.Find(name) is null)\n    nameScope.Register(name, element);\nelse if (nameScope.Find(name) != element)\n    throw new InvalidOperationException($\"Name '{name}' is taken by a different element.\");","typeGuard":null,"tryCatchPattern":"try { nameScope.Register(name, element); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"already registered\"))\n{\n    // pick a unique name or reuse the existing element\n}","preventionTips":["Keep x:Name values unique within each NameScope/template.","Before programmatic Register, check Find(name) for collisions.","Lint XAML for duplicate x:Name values in CI."],"tags":["namescope","duplicate-name","xaml","controls"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}