{"record":{"id":"41570598476cd219","repo":"dotnet/maui","slug":"xc0127","errorCode":"XC0127","errorMessage":"x:Key expects a string literal.","messagePattern":"x:Key expects a string literal\\.","errorType":"exception","errorClass":"BuildException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs","lineNumber":1932,"sourceCode":"\n\t\t\tif (paramType.FullName == \"System.Object\" && varValue.VariableType.IsValueType)\n\t\t\t\treturn true;\n\n\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","sourceCodeStart":1914,"sourceCodeEnd":1950,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs#L1914-L1950","documentation":"Thrown inside CanAddToResourceDictionary when an entry has an x:Key but its node is not a ValueNode (i.e. not a string literal). The compiler requires ResourceDictionary keys to be plain string literals; a markup/element key is rejected.","triggerScenarios":"Setting x:Key to a markup extension or nested element, e.g. `x:Key=\"{StaticResource KeyRes}\"` instead of a literal string.","commonSituations":"Developer tries to indirect the key through a resource or binding; copy-paste of a markup extension into x:Key.","solutions":["Use a plain string literal for x:Key.","If you need a shared/constant key, define it as a literal string (e.g. `x:Key=\"myKey\"`).","Do not place markup extensions or nested elements in x:Key."],"exampleFix":"<!-- before -->\n<x:String x:Key=\"{StaticResource KeyRes}\">Hi</x:String>\n\n<!-- after -->\n<x:String x:Key=\"greetingKey\">Hi</x:String>","handlingStrategy":"validation","validationCode":"// x:Key must be a plain string literal; reject any markup/element form\nstatic bool IsLiteralXKey(string raw) => !string.IsNullOrWhiteSpace(raw) && !raw.StartsWith(\"{\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set x:Key to a literal string.","Never place markup extensions or nested elements in x:Key.","Keep keys short, stable, and unique within the ResourceDictionary."],"tags":["xaml","xamlc","resource-dictionary","x-key"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}