dotnet/wpf · error · KeyNotFoundException
SR.Format(SR.DirectiveNotFound, name, this.TargetNamespace)
Error message
SR.Format(SR.DirectiveNotFound, name, this.TargetNamespace)
What it means
XaslNamespace.GetDirectiveType looks up a directive name in its DirectiveTypes dictionary and throws KeyNotFoundException(SR.Format(SR.DirectiveNotFound, name, TargetNamespace)) when the directive is not registered. The code comment notes this is an early-version behavior and the error path is expected to improve.
Solutions
- Fix the directive name in the XAML or call site to match a registered directive for this TargetNamespace.
- Verify the XaslNamespace instance you are querying actually owns the directive (check TargetNamespace in the message).
- Use GetAllDirectiveTypes() first to enumerate valid names before lookup.
- Wrap the lookup in try/catch for KeyNotFoundException if unknown directives are expected.
Example fix
// before
var t = xamlNamespace.GetDirectiveType("x:Shareable");
// after
var t = xamlNamespace.GetAllDirectiveTypes().Any(d => /* name matches */)
? xamlNamespace.GetDirectiveType("x:Shared")
: null; Defensive patterns
Strategy: validation
Validate before calling
bool exists = xamlNamespace.GetAllDirectiveTypes()
.Any(d => /* d matches requested name */);
if (!exists) throw new ArgumentException($"Unknown directive '{name}' in {xamlNamespace.TargetNamespace}"); Try / catch
try { var t = ns.GetDirectiveType(name); } catch (KeyNotFoundException ex) { /* handle unknown directive for ns.TargetNamespace */ } Prevention
- Validate directive names against GetAllDirectiveTypes() first
- Confirm the TargetNamespace owns the directive
- Fix XAML spelling of x: directives
When it happens
Trigger: Requesting a directive XamlType via GetDirectiveType(name) where name is not present in the namespace's DirectiveTypes, e.g. a typo'd directive like 'x:Ky' or a directive from a different xaml namespace.
Common situations: Hand-written XAML with misspelled x: directives; parsing documents referencing directives from a namespace whose XaslNamespace was built without that directive registered; version drift where a directive was added in a newer schema.
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
- ' '.' ' is a property without a getter and is not a valid…
- ' '.' ' is a property without a getter and is not a valid…
- ' '.' ' is a property without a getter and is not a valid…
- ' ' is not a valid XAML member name.
- Animation_ChildMustBeKeyFrame
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/7964f5a0795985a1.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XaslNamespace.cs:114
{
return null;
}
}
return xaslType;
}
public override IEnumerable<XamlType> GetAllXamlTypes()
{
return this.Types as IEnumerable<XamlType>;
}
public override XamlType GetDirectiveType(string name)
{
XaslType dirType = null;
if (!DirectiveTypes.TryGetValue(name, out dirType))
{
// throw in the early versions, do something better for error path.
throw new KeyNotFoundException(SR.Format(SR.DirectiveNotFound, name, this.TargetNamespace));
}
return dirType;
}
public override IEnumerable<XamlType> GetAllDirectiveTypes()
{
return DirectiveTypes as IEnumerable<XamlType>;
}
public override XamlProperty GetDirectiveProperty(string name)
{
XaslMember dirProperty = null;
if (!DirectiveProperties.TryGetValue(name, out dirProperty))
{
// throw in the early versions, do something better for error path.
throw new KeyNotFoundException(SR.Format(SR.DirectiveNotFound, name, this.TargetNamespace));
}
return dirProperty;View on GitHub (pinned to 81131a70a4)