dotnet/wpf · error · InvalidOperationException
SR.Format(SR.MarkupExtensionTypeNameBad, _typeName)
Error message
SR.Format(SR.MarkupExtensionTypeNameBad, _typeName)
What it means
TypeExtension.ProvideValue throws InvalidOperationException when the IXamlTypeResolver.Resolve(_typeName) call returns null, i.e. the type-name string could not be resolved to any known type. The extension surfaces the offending _typeName in the message via SR.MarkupExtensionTypeNameBad.
Solutions
- Fix the TypeName string to match a resolvable type name, including the correct XML namespace prefix.
- Add or correct the xmlns/clr-namespace mapping at the top of the XAML file.
- Verify the type's assembly is referenced and the type is public.
- Check for renames after refactoring and update the XAML reference.
Example fix
// before
<Window xmlns:local="clr-namespace:App.Views">
<Button Tag="{x:Type local:MyControll}" /> <!-- typo -->
// after
<Window xmlns:local="clr-namespace:App.Views">
<Button Tag="{x:Type local:MyControl}" /> Defensive patterns
Strategy: validation
Validate before calling
// before writing {x:Type prefix:Name} in XAML, verify:
// 1) xmlns:prefix mapping exists and points to the right clr-namespace/assembly
// 2) the type name is spelled correctly and is public Try / catch
try { value = ext.ProvideValue(provider); }
catch (InvalidOperationException ex) { throw new XamlLoadException($"Cannot resolve type '{ext.TypeName}'. Check xmlns mappings and assembly references.", ex); } Prevention
- Compile-time check: reference types from code-behind to catch renames.
- Keep clr-namespace mappings in one reviewed location.
- Run XAML compilation (XamlC / BAML) in CI so unresolved types fail the build.
When it happens
Trigger: Setting TypeName to a string that the XAML type resolver cannot resolve — misspelled type name, missing xmlns prefix mapping, type not in referenced assemblies — then calling ProvideValue during a XAML parse.
Common situations: Typos in x:Type markup usage like {x:Type local:MyControll}; missing clr-namespace/xmlns declaration in the XAML file; renamed or moved type after a refactor; assembly not referenced by the project.
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
- ArgumentNullException(nameof(localName))
- ArgumentNullException(nameof(xmlNamespace))
- IAmbientProvider
- IXamlSchemaContextProvider
- NotSupportedException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/04ad44a6983e57d9.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtension.cs:78
// Validate the initialization.
if (_typeName is null)
{
throw new InvalidOperationException(SR.MarkupExtensionTypeName);
}
// Get the IXamlTypeResolver from the service provider
ArgumentNullException.ThrowIfNull(serviceProvider);
if (serviceProvider.GetService(typeof(IXamlTypeResolver)) is not IXamlTypeResolver xamlTypeResolver)
{
throw new InvalidOperationException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, nameof(IXamlTypeResolver)));
}
// Get the type
_type = xamlTypeResolver.Resolve(_typeName);
if (_type is null)
{
throw new InvalidOperationException(SR.Format(SR.MarkupExtensionTypeNameBad, _typeName));
}
return _type;
}
/// <summary>
/// The typename represented by this markup extension. The name has the format
/// Prefix:Typename, where Prefix is the xml namespace prefix for this type.
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string TypeName
{
get => _typeName;
set
{
// Reset the type so ProvideValue does not use the existing type.
_typeName = value ?? throw new ArgumentNullException(nameof(value));
_type = null;View on GitHub (pinned to 81131a70a4)