{"record":{"id":"a0bb0fb57c44a76e","repo":"dotnet/wpf","slug":"invalidoperationexception-sr-format-sr-prefixnotinframes","errorCode":null,"errorMessage":"InvalidOperationException(SR.Format(SR.PrefixNotInFrames, prefix))","messagePattern":"InvalidOperationException\\(SR\\.Format\\(SR\\.PrefixNotInFrames, prefix\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":398,"sourceCode":"                prefixAssignmentHistory.Add(prefix, ns);\n            }\n        }\n\n        private bool IsShadowed(string ns, string prefix)\n        {\n            Debug.Assert(ns is not null);\n            Debug.Assert(prefix is not null);\n\n            string registeredNamespace;\n            foreach (Frame frame in namespaceScopes)\n            {\n                if (frame.TryLookupNamespace(prefix, out registeredNamespace))\n                {\n                    return (registeredNamespace != ns);\n                }\n            }\n\n            throw new InvalidOperationException(SR.Format(SR.PrefixNotInFrames, prefix));\n        }\n\n        //\n        // FindPrefix attempts to look up existing prefixes for the namespaces;\n        // if none is found, it will define one for the first namespace in the list.\n        // Caveat: if the prefix found is shadowed (by a re-definition), FindPrefix will\n        // redefine it.\n        //\n        private string FindPrefix(IList<string> namespaces, out string chosenNamespace)\n        {\n            string prefix = LookupPrefix(namespaces, out chosenNamespace);\n\n            if (prefix is null)\n            {\n                chosenNamespace = namespaces[0];\n                prefix = DefinePrefix(chosenNamespace);\n                AssignNamespacePrefix(chosenNamespace, prefix);\n            }","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L380-L416","documentation":"XamlXmlWriter.IsShadowed determines whether a prefix currently maps to a different namespace by walking the writer's namespace frames. If the prefix is not found in any frame, an internal invariant has been violated (callers are expected to look up only known prefixes), so an InvalidOperationException is thrown.","triggerScenarios":"FindPrefix (or WriteUndefinedNamespaces / TypeArgumentsContainNamespaceThatNeedsDefinition / WriteStartMember) calls IsShadowed with a prefix that was never registered via AssignNamespacePrefix or the default namespace initialization — typically after a partial/custom state setup or a bug in prefix bookkeeping.","commonSituations":"Custom writer pipelines that reset or manipulate writer state mid-stream; calling writer APIs out of order so frames were never pushed; library-level bugs surfaced during XAML node-stream writing.","solutions":["Verify XAML write calls follow a valid node-stream order (object → members → end object)","Ensure WriteNamespace/prefix assignments happen before members referencing those namespaces are written","Check whether you are reusing or resetting a XamlXmlWriter across documents incorrectly; create a new writer instead","If reproducible with valid input, report as a bug to the WPF/System.Xaml maintainers"],"exampleFix":"// before\nwriter.WriteStartMember(member); // member's type namespace never declared -> frames empty\n// after\nwriter.WriteNamespace(new NamespaceDeclaration(member.DeclaringType.PreferredXamlNamespace, \"p\"));\nwriter.WriteStartMember(member);","handlingStrategy":"try-catch","validationCode":"// Ensure namespaces used by members are declared before writing members:\nwriter.WriteNamespace(new NamespaceDeclaration(member.DeclaringType.PreferredXamlNamespace, \"p\"));","typeGuard":null,"tryCatchPattern":"try { writer.WriteStartMember(member); }\ncatch (InvalidOperationException) { /* prefix bookkeeping broken; restart with fresh writer and correct call order */ }","preventionTips":["Follow valid node-stream order; declare namespaces before use","Do not reset or share writer state across documents","Treat this as an internal invariant; reproducible cases are library bugs worth reporting"],"tags":["xaml","invalid-operation","internal-state"],"backgroundTag":"internal-invariant-violation","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"}