dotnet/maui · error · BuildException
XC0040
XC0040
Error message
Cannot convert value "{0}" to "{1}". What it means
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.
Source
Thrown at src/Controls/src/Build.Tasks/CompiledConverters/EnumTypeConverter.cs:22
using Microsoft.Maui.Controls.Xaml;
using Mono.Cecil.Cil;
namespace Microsoft.Maui.Controls.XamlC
{
class EnumTypeConverter<TEnum> : ICompiledTypeConverter where TEnum : struct
{
public IEnumerable<Instruction> ConvertFromString(string value, ILContext context, BaseNode node)
{
if (!string.IsNullOrEmpty(value))
{
value = value.Trim();
if (Enum.TryParse(value, out TEnum enumValue))
{
yield return Instruction.Create(OpCodes.Ldc_I4, (int)(object)enumValue);
yield break;
}
}
throw new BuildException(BuildExceptionCode.Conversion, node, null, value, typeof(TEnum));
}
}
}View on GitHub (pinned to f377ff1c5e)
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.
Example fix
<!-- before --> <Label HorizontalTextAlignment="Middle" /> <!-- after --> <Label HorizontalTextAlignment="Center" />
Defensive patterns
Strategy: validation
Validate before calling
// Validate an enum string for a known enum type before building
static bool IsValidEnumValue<TEnum>(string value) where TEnum : struct
{
return !string.IsNullOrWhiteSpace(value)
&& Enum.TryParse<TEnum>(value.Trim(), out _);
} Type guard
static bool IsEnumValue<TEnum>(string value, out TEnum result) where TEnum : struct
=> Enum.TryParse(value?.Trim(), out result); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/c302c38125e58ef0.
Report an issue: GitHub.