dotnet/wpf · error · XamlParseException

SR.Format(SR.PrefixNotFound, typeName.Prefix)

Error message

SR.Format(SR.PrefixNotFound, typeName.Prefix)

What it means

GetXamlTypeName resolves a XamlName's prefix to a XAML namespace before constructing a XamlTypeName; if ResolveXamlNameNS returns null the prefix is unknown and XamlParseException(PrefixNotFound, prefix) is thrown. Called by fullTypeName during type-name resolution.

Solutions

  1. Declare the prefix: add xmlns:local="clr-namespace:...;assembly=..." to the root element.
  2. Fix the prefix in the type reference to match a declared namespace.
  3. Ensure generated/dynamic XAML emits all needed xmlns declarations.
  4. Catch XamlParseException, parse the prefix from the message, and validate all prefixes against declared namespaces before parsing.

Example fix

// before
<Button Style="{styles:MyStyle}"/> <!-- styles undeclared -->
// after
<Window xmlns:styles="clr-namespace:App.Styles;assembly=App" ...>
  <Button Style="{styles:MyStyle}"/>
</Window>
Defensive patterns

Strategy: validation

Validate before calling

// validate every prefix in type references is declared before resolution
bool prefixDeclared = root.Attributes().Any(a => a.IsNamespaceDeclaration && a.Name.LocalName == typeName.Prefix);
if (!prefixDeclared) throw new XamlParseException($"Undeclared prefix '{typeName.Prefix}' in type reference");

Type guard

bool CanResolvePrefix(XmlNamespaceManager nsMgr, string prefix) => prefix == "" || nsMgr.LookupNamespace(prefix) != null;

Try / catch

try { var tn = helper.FullTypeName(name); }
catch (XamlParseException ex) when (ex.Message.Contains("prefix")) { // undeclared prefix on type reference
  throw new FormatException("Add xmlns declaration for prefix: " + ex.Message, ex); }

Prevention

When it happens

Trigger: Resolving a type reference such as x:Type extensions={x:Static ...} or MarkupExtension type references where the prefix in the type name has no matching xmlns declaration in scope.

Common situations: Typos in prefixes on type usages (e.g. {local:Converter} without xmlns:local); missing xmlns on the root element; XAML moved between files losing namespace scope; dynamic XAML strings generated without emitted xmlns declarations.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/XamlContext.cs:247

        }

        internal XamlType GetXamlType(XamlName typeName)
        {
            return GetXamlType(typeName, false);
        }

        internal XamlType GetXamlType(XamlName typeName, bool returnUnknownTypesOnFailure)
        {
            XamlTypeName fullTypeName = GetXamlTypeName(typeName);
            return GetXamlType(fullTypeName, returnUnknownTypesOnFailure);
        }

        internal XamlTypeName GetXamlTypeName(XamlName typeName)
        {
            string xamlNs = ResolveXamlNameNS(typeName);
            if (xamlNs is null)
            {
                throw new XamlParseException(SR.Format(SR.PrefixNotFound, typeName.Prefix));
            }

            return new XamlTypeName(xamlNs, typeName.Name);
        }

        internal XamlType GetXamlType(XamlTypeName typeName)
        {
            return GetXamlType(typeName, false, false);
        }

        internal XamlType GetXamlType(XamlTypeName typeName, bool returnUnknownTypesOnFailure)
        {
            return GetXamlType(typeName, returnUnknownTypesOnFailure, false);
        }

        internal XamlType GetXamlType(XamlTypeName typeName, bool returnUnknownTypesOnFailure,
            bool skipVisibilityCheck)
        {

View on GitHub (pinned to 81131a70a4)