{"record":{"id":"df6faa13f515b9dd","repo":"dotnet/maui","slug":"xc0125","errorCode":"XC0125","errorMessage":"\"A resource with the key \"{0}\" is already present in the ResourceDictionary.","messagePattern":"\"A resource with the key \"(.+?)\" is already present in the ResourceDictionary\\.","errorType":"exception","errorClass":"BuildException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs","lineNumber":1936,"sourceCode":"\t\t\treturn CanAddToResourceDictionary(parent, propertyType, elementNode, lineInfo, context);\n\t\t}\n\n\t\tstatic bool CanAddToResourceDictionary(VariableDefinition parent, TypeReference collectionType, ElementNode node, IXmlLineInfo lineInfo, ILContext context)\n\t\t{\n\t\t\tif (parent is null)\n\t\t\t\treturn false;\n\n\t\t\tif (collectionType.FullName != \"Microsoft.Maui.Controls.ResourceDictionary\"\n\t\t\t\t&& !collectionType.InheritsFromOrImplements(context.Cache, context.Module.ImportReference(context.Cache, (\"Microsoft.Maui.Controls\", \"Microsoft.Maui.Controls\", \"ResourceDictionary\"))))\n\t\t\t\treturn false;\n\n\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;","sourceCodeStart":1918,"sourceCodeEnd":1954,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs#L1918-L1954","documentation":"Raised by the XAML compiler (Build.Tasks) when two resources inside the same ResourceDictionary declare the same x:Key. The build task keeps a name set via GetResourceNamesInUse(parent) and rejects any key that already appears in it, so the XAML cannot compile until the collision is removed.","triggerScenarios":"The SetPropertiesVisitor sees a node whose parent collection is (or inherits from) Microsoft.Maui.Controls.ResourceDictionary, the node carries an x:Key whose string value is already present in context.Cache.GetResourceNamesInUse(parent), and the duplicate key throw fires.","commonSituations":"Copying a Style/Color/OnPlatform block and forgetting to rename its x:Key; merging MergedDictionaries that both define 'DefaultStyle'; refactoring resources between App.xaml and page-level dictionaries where the same key leaks in.","solutions":["Search the whole XAML file (and any MergedDictionaries) for the duplicate x:Key and rename one of the two occurrences to a unique value.","If the duplicate comes from a merged dictionary, override it intentionally with MergedWith/Source semantics or remove it from the source dictionary instead of redeclaring.","Use a unique prefix per feature area (e.g. 'LoginPrimaryButton') to avoid accidental collisions as dictionaries grow."],"exampleFix":"<!-- before -->\n<ResourceDictionary>\n    <x:String x:Key=\"Greeting\">Hello</x:String>\n    <x:String x:Key=\"Greeting\">Hi</x:String>\n</ResourceDictionary>\n<!-- after -->\n<ResourceDictionary>\n    <x:String x:Key=\"Greeting\">Hello</x:String>\n    <x:String x:Key=\"GreetingAlt\">Hi</x:String>\n</ResourceDictionary>","handlingStrategy":"validation","validationCode":"// Before shipping XAML, assert every x:Key in a ResourceDictionary is unique.\n// Pseudo: gather all x:Key literal strings per RD scope and assert no duplicates.\nstatic void AssertUniqueKeys(IEnumerable<string> keys)\n{\n    var dup = keys.GroupBy(k => k).Where(g => g.Count() > 1).Select(g => g.Key);\n    if (dup.Any()) throw new InvalidOperationException(\"Duplicate keys: \" + string.Join(\", \", dup));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run the XAML compiler (XC0125) as part of CI so duplicate keys fail the build before runtime.","Adopt a key-naming convention per feature area to avoid accidental collisions across merged dictionaries.","When merging ResourceDictionaries, grep for the new keys against the destination before merging."],"tags":["xaml","resource-dictionary","build-task","duplicate-key","xc0125"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}