dotnet/wpf · error · InvalidOperationException

InvalidOperationException(SR.Format(SR.PrefixNotInFrames…

Error message

InvalidOperationException(SR.Format(SR.PrefixNotInFrames, prefix))

What it means

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.

Solutions

  1. Verify XAML write calls follow a valid node-stream order (object → members → end object)
  2. Ensure WriteNamespace/prefix assignments happen before members referencing those namespaces are written
  3. Check whether you are reusing or resetting a XamlXmlWriter across documents incorrectly; create a new writer instead
  4. If reproducible with valid input, report as a bug to the WPF/System.Xaml maintainers

Example fix

// before
writer.WriteStartMember(member); // member's type namespace never declared -> frames empty
// after
writer.WriteNamespace(new NamespaceDeclaration(member.DeclaringType.PreferredXamlNamespace, "p"));
writer.WriteStartMember(member);
Defensive patterns

Strategy: try-catch

Validate before calling

// Ensure namespaces used by members are declared before writing members:
writer.WriteNamespace(new NamespaceDeclaration(member.DeclaringType.PreferredXamlNamespace, "p"));

Try / catch

try { writer.WriteStartMember(member); }
catch (InvalidOperationException) { /* prefix bookkeeping broken; restart with fresh writer and correct call order */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/a0bb0fb57c44a76e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:398

                prefixAssignmentHistory.Add(prefix, ns);
            }
        }

        private bool IsShadowed(string ns, string prefix)
        {
            Debug.Assert(ns is not null);
            Debug.Assert(prefix is not null);

            string registeredNamespace;
            foreach (Frame frame in namespaceScopes)
            {
                if (frame.TryLookupNamespace(prefix, out registeredNamespace))
                {
                    return (registeredNamespace != ns);
                }
            }

            throw new InvalidOperationException(SR.Format(SR.PrefixNotInFrames, prefix));
        }

        //
        // FindPrefix attempts to look up existing prefixes for the namespaces;
        // if none is found, it will define one for the first namespace in the list.
        // Caveat: if the prefix found is shadowed (by a re-definition), FindPrefix will
        // redefine it.
        //
        private string FindPrefix(IList<string> namespaces, out string chosenNamespace)
        {
            string prefix = LookupPrefix(namespaces, out chosenNamespace);

            if (prefix is null)
            {
                chosenNamespace = namespaces[0];
                prefix = DefinePrefix(chosenNamespace);
                AssignNamespacePrefix(chosenNamespace, prefix);
            }

View on GitHub (pinned to 81131a70a4)