{"record":{"id":"c302c38125e58ef0","repo":"dotnet/maui","slug":"xc0040-c302c3","errorCode":"XC0040","errorMessage":"Cannot convert value \"{0}\" to \"{1}\".","messagePattern":"Cannot convert value \"(.+?)\" to \"(.+?)\"\\.","errorType":"error_code","errorClass":"BuildException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Build.Tasks/CompiledConverters/EnumTypeConverter.cs","lineNumber":22,"sourceCode":"using Microsoft.Maui.Controls.Xaml;\nusing Mono.Cecil.Cil;\n\nnamespace Microsoft.Maui.Controls.XamlC\n{\n\tclass EnumTypeConverter<TEnum> : ICompiledTypeConverter where TEnum : struct\n\t{\n\t\tpublic IEnumerable<Instruction> ConvertFromString(string value, ILContext context, BaseNode node)\n\t\t{\n\t\t\tif (!string.IsNullOrEmpty(value))\n\t\t\t{\n\t\t\t\tvalue = value.Trim();\n\t\t\t\tif (Enum.TryParse(value, out TEnum enumValue))\n\t\t\t\t{\n\t\t\t\t\tyield return Instruction.Create(OpCodes.Ldc_I4, (int)(object)enumValue);\n\t\t\t\t\tyield break;\n\t\t\t\t}\n\t\t\t}\n\t\t\tthrow new BuildException(BuildExceptionCode.Conversion, node, null, value, typeof(TEnum));\n\t\t}\n\t}\n}","sourceCodeStart":4,"sourceCodeEnd":25,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Build.Tasks/CompiledConverters/EnumTypeConverter.cs#L4-L25","documentation":"XC0040 Conversion thrown by the generic EnumTypeConverter<TEnum> when a XAML string cannot be parsed as the target enum type. The converter trims the value and calls Enum.TryParse<TEnum>. If the value is null/empty or does not match any defined enum member (by name), the build-time exception fires.","triggerScenarios":"Setting an enum-backed property to a string that is not a member of the enum. For example, setting a TextAlignment to 'Justify' when the enum only defines Start, Center, End. Using integer values like '2' can work via TryParse but non-numeric, non-member strings always fail.","commonSituations":"Misspelling an enum member (e.g. 'Centr' instead of 'Center'). Using values from a different/older API version where the enum member was renamed or removed. Copying values from documentation for a different platform or framework.","solutions":["Use the exact enum member name as defined in the target enum type (case-sensitive on most platforms, though TryParse is case-sensitive by default here).","Check the IntelliSense or the enum definition for valid member names.","Ensure the value is not empty, null, or containing only whitespace."],"exampleFix":"<!-- before -->\n<Label HorizontalTextAlignment=\"Middle\" />\n\n<!-- after -->\n<Label HorizontalTextAlignment=\"Center\" />","handlingStrategy":"validation","validationCode":"// Validate an enum string for a known enum type before building\nstatic bool IsValidEnumValue<TEnum>(string value) where TEnum : struct\n{\n    return !string.IsNullOrWhiteSpace(value)\n        && Enum.TryParse<TEnum>(value.Trim(), out _);\n}","typeGuard":"static bool IsEnumValue<TEnum>(string value, out TEnum result) where TEnum : struct\n    => Enum.TryParse(value?.Trim(), out result);","tryCatchPattern":null,"preventionTips":["Cross-check enum member names against the type definition or IntelliSense before writing them in XAML.","Enum.TryParse is case-sensitive in this converter — match the exact casing from the enum definition.","Avoid using integer values for enums in XAML; prefer named members for readability and resilience."],"tags":["xaml","maui","xamlc","type-converter","enum","build-time"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}