dotnet/wpf · error · XamlParseException

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

Error message

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

What it means

GetDottedProperty resolves the namespace of a dotted property name via ResolveXamlNameNS; when the prefix cannot be mapped to a namespace, it throws XamlParseException with PrefixNotFound carrying the unknown prefix.

Solutions

  1. Add the missing xmlns declaration: xmlns:controls="clr-namespace:MyNs;assembly=MyAssembly" on the root or nearest element.
  2. Correct the prefix in the dotted name to match an existing declaration.
  3. If the prefix maps to an assembly not referenced, add the project/package reference.
  4. Catch XamlParseException, extract the prefix from the message, and report the undeclared namespace to the author.

Example fix

// before
<StackPanel><local:Widget.Label>hi</local:Widget.Label></StackPanel> <!-- local undeclared -->
// after
<StackPanel xmlns:local="clr-namespace:App.Views;assembly=App">
  <local:Widget.Label>hi</local:Widget.Label>
</StackPanel>
Defensive patterns

Strategy: validation

Validate before calling

// ensure all prefixes used in element/attribute names are declared
var declared = root.Attributes().Where(a => a.IsNamespaceDeclaration).Select(a => a.Name.LocalName);
var used = doc.Descendants().Attributes().SelectMany(a => a.Name.NamespaceName == "" ? new[]{a.Name.LocalName.Split('.').First().Split(':').First()} : Enumerable.Empty<string>());
var missing = used.Except(declared).ToList();

Type guard

bool PrefixDeclared(XDocument doc, string prefix) => doc.Root.Attributes().Any(a => a.IsNamespaceDeclaration && a.Name.LocalName == prefix);

Try / catch

try { LoadXaml(xamlText); }
catch (XamlParseException ex) when (ex.Message.Contains("prefix")) { // undeclared xmlns prefix
  throw new FormatException("Declare the missing xmlns prefix: " + ex.Message, ex); }

Prevention

When it happens

Trigger: A dotted property attribute/element uses an xmlns prefix that is not declared (e.g. <controls:Foo.Text> with no xmlns:controls declaration in scope).

Common situations: Copy-pasting XAML snippets into another file without copying the xmlns declarations; renaming a prefix in one place but not others; merged XAML fragments missing namespace imports.

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/90d406ceecb3a078. Report an issue: GitHub.

Appendix: source

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

        /// </summary>
        /// <param name="tagType">The xamlType of the enclosing Tag</param>
        /// <param name="tagNamespace">The namespace of the enclosing Tag</param>
        /// <param name="propName">The dotted name of the property</param>
        /// <param name="tagIsRoot">Whether the tag is the root of the document</param>
        /// <returns></returns>
        public XamlMember GetDottedProperty(XamlType tagType, string tagNamespace, XamlPropertyName propName, bool tagIsRoot)
        {
            if (tagType is null)
            {
                throw new XamlInternalException(SR.Format(SR.ParentlessPropertyElement, propName.ScopedName));
            }

            XamlMember property = null;
            XamlType ownerType = null;
            string ns = ResolveXamlNameNS(propName);
            if (ns is null)
            {
                throw new XamlParseException(SR.Format(SR.PrefixNotFound, propName.Prefix));
            }

            XamlType rootTagType = tagIsRoot ? tagType : null;

            // If we have <foo x:TA="" foo.bar=""/> we want foo in foo.bar to match the tag
            // type since there is no way to specify generic syntax in dotted property notation
            // If that fails, then we fall back to the non-generic case below.
            bool ownerTypeMatchesGenericTagType = false;
            if (tagType.IsGeneric)
            {
                ownerTypeMatchesGenericTagType = PropertyTypeMatchesGenericTagType(tagType, tagNamespace, ns, propName.OwnerName);
                if (ownerTypeMatchesGenericTagType)
                {
                    property = GetInstanceOrAttachableProperty(tagType, propName.Name, rootTagType);
                    if (property is not null)
                    {
                        return property;
                    }

View on GitHub (pinned to 81131a70a4)