{"record":{"id":"da1844b95eb814df","repo":"dotnet/wpf","slug":"sr-namescopeonrootinstance","errorCode":null,"errorMessage":"SR.NameScopeOnRootInstance","messagePattern":"SR\\.NameScopeOnRootInstance","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs","lineNumber":883,"sourceCode":"                if (instance == frame.Instance)\n                {\n                    return true;\n                }\n\n                frame = (ObjectWriterFrame)frame.Previous;\n            }\n\n            return false;\n        }\n\n        private XAML3.INameScopeDictionary HuntAroundForARootNameScope(ObjectWriterFrame rootFrame)\n        {\n            Debug.Assert(rootFrame.Depth == 1);\n\n            object inst = rootFrame.Instance;\n            if (inst is null && rootFrame.XamlType.IsNameScope)\n            {\n                throw new InvalidOperationException(SR.NameScopeOnRootInstance);\n            }\n\n            XAML3.INameScopeDictionary nameScopeDictionary = null;\n\n            nameScopeDictionary = inst as XAML3.INameScopeDictionary;\n\n            if (nameScopeDictionary is null)\n            {\n                if (inst is XAML3.INameScope nameScope)\n                {\n                    nameScopeDictionary = new NameScopeDictionary(nameScope);\n                }\n            }\n\n            // If the root instance isn't a name scope\n            // then perhaps it designated a property as the name scope.\n            if (nameScopeDictionary is null)\n            {","sourceCodeStart":865,"sourceCodeEnd":901,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs#L865-L901","documentation":"The ObjectWriterContext constructor sets up the name scope for the root object frame. If the root frame has no instance yet and its XamlType IsNameScope (i.e. the root itself represents a name scope such as x:NameScope semantics), the writer cannot establish a name scope dictionary and throws InvalidOperationException with SR.NameScopeOnRootInstance.","triggerScenarios":"Running XamlObjectWriter against a XAML tree whose root element is a name-scope type but has a null instance at depth 1 — e.g. writing XAML where the root is declared as a name scope without an instantiable root object.","commonSituations":"Custom XAML writing pipelines that provide no root instance while the root type is flagged IsNameScope; misuse of x:XData/x:NameScope-style roots; generated XAML whose root object failed to instantiate earlier in the pipeline.","solutions":["Ensure the root object is instantiated before the writer's name-scope setup — supply a valid root instance (XamlObjectWriterSettings.RootObjectInstance) or fix the preceding creation step.","Do not make the root element a name-scope type; wrap it in a concrete root object (e.g. Page, UserControl).","Check earlier writer errors: a failed root instantiation leaving Instance null is often the underlying cause — fix that error first."],"exampleFix":"// before: root type is a name scope and instance is null\nvar writer = new XamlObjectWriter(schemaContext);\n// after: provide a concrete root instance\nvar settings = new XamlObjectWriterSettings { RootObjectInstance = new MyRoot() };\nvar writer = new XamlObjectWriter(schemaContext, settings);","handlingStrategy":"validation","validationCode":"if (rootInstance is null && schemaContext.GetXamlType(rootType).IsNameScope)\n    throw new InvalidOperationException(\"Root of a XAML tree cannot be a name-scope type with a null instance\");","typeGuard":"static bool HasValidRoot(object? root, XamlType type) => root is not null || !type.IsNameScope;","tryCatchPattern":"try { RunObjectWriter(xamlReader); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"name scope\"))\n{ /* supply RootObjectInstance or change the root element type */ }","preventionTips":["Always give XamlObjectWriter a concrete, instantiable root (via RootObjectInstance or the stream's root element).","Do not use name-scope pseudo-types as the document root of XAML you write at runtime.","Handle earlier instantiation exceptions so a null root instance never reaches the writer."],"tags":["wpf","xaml","namescope","invalid-state","root-instance"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}