dotnet/wpf · error · NotImplementedException

NotImplementedException

Error message

NotImplementedException

What it means

TemplateContent's internal service provider implements IXamlNamespaceResolver but only supports GetNamespace(prefix); GetNamespacePrefixes() is deliberately unimplemented and always throws NotImplementedException. Any consumer asking for the full prefix enumeration gets this exception by design.

Solutions

  1. Avoid calling GetNamespacePrefixes() inside deferred template content; use GetNamespace(prefix) with a known prefix instead.
  2. Obtain namespace prefixes from the outer/primary XAML parser context rather than the template's service provider.
  3. Guard the call in try/catch on NotImplementedException when the service provider is unknown.

Example fix

// before
var prefixes = ((IXamlNamespaceResolver)services.GetService(typeof(IXamlNamespaceResolver))).GetNamespacePrefixes();
// after
var nsResolver = (IXamlNamespaceResolver)services.GetService(typeof(IXamlNamespaceResolver));
var ns = nsResolver.GetNamespace("x"); // only GetNamespace is supported
Defensive patterns

Strategy: try-catch

Validate before calling

var svc = services.GetService(typeof(IXamlNamespaceResolver)) as IXamlNamespaceResolver;
bool hasService = svc != null; // then prefer GetNamespace(prefix) only

Type guard

static bool SupportsPrefixEnumeration(IXamlNamespaceResolver r) { try { r.GetNamespacePrefixes(); return true; } catch (NotImplementedException) { return false; } }

Try / catch

try { prefixes = nsResolver.GetNamespacePrefixes(); }
catch (NotImplementedException) { prefixes = Enumerable.Empty<NamespaceDeclaration>(); }

Prevention

When it happens

Trigger: Calling IXamlNamespaceResolver.GetNamespacePrefixes() on the service object obtained from TemplateContent's IServiceProvider (e.g. via GetService(typeof(IXamlNamespaceResolver))).

Common situations: Custom markup extensions, value serializers, or schema context code that enumerates all namespace prefixes while template content is being deferred-parsed.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateContent.cs:1481

            {
                FrugalObjectList<NamespaceDeclaration> namespaces = Frames.InScopeNamespaces;
                if (namespaces != null)
                {
                    for (int idx = 0; idx < namespaces.Count; idx++)
                    {
                        if (namespaces[idx].Prefix == prefix)
                        {
                            return namespaces[idx].Namespace;
                        }
                    }
                }

                return ((IXamlNamespaceResolver)_services.GetService(typeof(IXamlNamespaceResolver))).GetNamespace(prefix);
            }

            IEnumerable<NamespaceDeclaration> IXamlNamespaceResolver.GetNamespacePrefixes()
            {
                throw new NotImplementedException();
            }

            #endregion

            #region ITypeDescriptorContext Members

            IContainer ITypeDescriptorContext.Container
            {
                get { return null; }
            }

            object ITypeDescriptorContext.Instance
            {
                get { return null; }
            }

            void ITypeDescriptorContext.OnComponentChanged()
            {

View on GitHub (pinned to 81131a70a4)