{"record":{"id":"f528d245d6251f0b","repo":"dotnet/maui","slug":"xc0126","errorCode":"XC0126","errorMessage":"Resources in ResourceDictionary require a x:Key attribute.","messagePattern":"Resources in ResourceDictionary require a x:Key attribute\\.","errorType":"exception","errorClass":"BuildException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs","lineNumber":1948,"sourceCode":"\t\t\tif (node.Properties.TryGetValue(XmlName.xKey, out INode value))\n\t\t\t{\n\t\t\t\tvar valueNode = value as ValueNode ?? throw new BuildException(XKeyNotLiteral, lineInfo, null);\n\t\t\t\tvar key = valueNode.Value as string;\n\t\t\t\tvar names = context.Cache.GetResourceNamesInUse(parent);\n\t\t\t\tif (names.Contains(key))\n\t\t\t\t\tthrow new BuildException(ResourceDictDuplicateKey, lineInfo, null, key);\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\t//is there a RD.Add() overrides that accepts this ?\n\t\t\tvar nodeTypeRef = context.Variables[node].VariableType;\n\t\t\tvar module = context.Body.Method.Module;\n\t\t\tif (module.ImportMethodReference(context.Cache,\n\t\t\t\t\t\t\t\t\t\t\t module.GetTypeDefinition(context.Cache, (\"Microsoft.Maui.Controls\", \"Microsoft.Maui.Controls\", \"ResourceDictionary\")),\n\t\t\t\t\t\t\t\t\t\t\t methodName: \"Add\",\n\t\t\t\t\t\t\t\t\t\t\t parameterTypes: [nodeTypeRef]) != null)\n\t\t\t\treturn true;\n\t\t\tthrow new BuildException(ResourceDictMissingKey, lineInfo, null);\n\t\t}\n\n\t\tstatic IEnumerable<Instruction> Add(VariableDefinition parent, XmlName propertyName, INode node, IXmlLineInfo iXmlLineInfo, ILContext context)\n\t\t{\n\t\t\tvar module = context.Body.Method.Module;\n\t\t\tvar elementNode = node as ElementNode;\n\t\t\tvar vardef = context.Variables[elementNode];\n\n\t\t\tTypeReference propertyType;\n\t\t\tforeach (var instruction in GetPropertyValue(parent, propertyName, context, iXmlLineInfo, out propertyType))\n\t\t\t\tyield return instruction;\n\n\t\t\tif (CanAddToResourceDictionary(parent, propertyType, elementNode, iXmlLineInfo, context))\n\t\t\t{\n\t\t\t\tforeach (var instruction in AddToResourceDictionary(parent, elementNode, iXmlLineInfo, context))\n\t\t\t\t\tyield return instruction;\n\t\t\t\tyield break;\n\t\t\t}","sourceCodeStart":1930,"sourceCodeEnd":1966,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs#L1930-L1966","documentation":"Raised by the XAML compiler when a node placed inside a ResourceDictionary has no x:Key attribute and its CLR type has no ResourceDictionary.Add(T) overload that would accept it implicitly. The task probes for an Add method whose single parameter matches the node's type; only when none exists does it throw, forcing an explicit key.","triggerScenarios":"A child element of a ResourceDictionary whose type is not one of the implicitly-keyed kinds (the task finds no matching Add overload) and which carries no XmlName.xKey property, so the final 'throw new BuildException(ResourceDictMissingKey...)' is reached.","commonSituations":"Declaring a Style without x:Key (styles in a RD are not implicitly keyed unless they are the implicit default style target); adding a custom type to a RD before giving the RD an Add(T) overload; pasting resources from a ContentPage.Resources scope where keys were optional into a ResourceDictionary where they are required.","solutions":["Add an x:Key attribute to the resource element, e.g. <Style x:Key=\"RedButton\" TargetType=\"Button\">.","If the type is meant to be implicitly addable, add a public Add(T) instance method to your ResourceDictionary subclass that accepts that type.","Move the resource out of the ResourceDictionary into a property/ContentPage.Resources context that does not require keys."],"exampleFix":"<!-- before -->\n<ResourceDictionary>\n    <Style TargetType=\"Button\">\n        <Setter Property=\"BackgroundColor\" Value=\"Red\"/>\n    </Style>\n</ResourceDictionary>\n<!-- after -->\n<ResourceDictionary>\n    <Style x:Key=\"RedButton\" TargetType=\"Button\">\n        <Setter Property=\"BackgroundColor\" Value=\"Red\"/>\n    </Style>\n</ResourceDictionary>","handlingStrategy":"validation","validationCode":"// Lint rule: any element directly under <ResourceDictionary> must either carry x:Key\n// or be a type for which ResourceDictionary.Add(T) exists (e.g. implicitly-keyed styles).\nstatic bool NeedsKey(string elementName) => elementName is \"Style\" or \"DataTemplate\";","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat 'element inside <ResourceDictionary>' as 'needs x:Key' unless you know the type has an implicit Add overload.","Keep a code-review checklist item: 'Resources in RD have x:Key'.","Let the XAML compiler catch it early in CI; do not silence XC0126."],"tags":["xaml","resource-dictionary","build-task","missing-key","xc0126"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}