dotnet/maui · error · BuildException

XC0040

XC0040

Error message

Cannot convert value "{0}" to "{1}".

What it means

XamlC error XC0040 from ColorTypeConverter. The compiled color converter accepts a '#'-prefixed hex string, the literal 'Default' (yields null), or the name of a member on Microsoft.Maui.Graphics.Colors (case-insensitive). Any other value throws XC0040.

Source

Thrown at src/Controls/src/Build.Tasks/CompiledConverters/ColorTypeConverter.cs:75

					if (fieldReference != null)
					{
						yield return Instruction.Create(OpCodes.Ldsfld, fieldReference);
						yield break;
					}

					var propertyGetterReference = module.ImportPropertyGetterReference(context.Cache, ("Microsoft.Maui.Graphics", "Microsoft.Maui.Graphics", "Colors"),
																					   color,
																					   isStatic: true,
																					   caseSensitive: false);

					if (propertyGetterReference != null)
					{
						yield return Instruction.Create(OpCodes.Call, propertyGetterReference);
						yield break;
					}
				}
			} while (false);
			throw new BuildException(BuildExceptionCode.Conversion, node, null, value, typeof(Color));
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Use '#'-prefixed hex (ARGB or RGB), e.g. BackgroundColor="#FF0000".
  2. If using a named color, verify it exists as a member of Microsoft.Maui.Graphics.Colors and spell it correctly (case-insensitive).
  3. Avoid CSS-style functions (rgb()/hsl()); convert them to hex first.

Example fix

<!-- before -->
<Label TextColor="darkgrey" />
<!-- 'darkgrey' is not a Colors member (only 'Gray'/'DarkGray'); note 'lightgrey' is special-cased -->

<!-- after -->
<Label TextColor="#A9A9A9" />
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: A Color-typed attribute is set to a value that is not hex, not 'Default', and not a Colors member — e.g. an HTML named color that is not in Maui's Colors, a typo, a localized name, or a non-color string.

Common situations: Using a CSS color name not present in Microsoft.Maui.Graphics.Colors; misspelling a color name; missing the '#' prefix; pasting an rgba()/hsl() string.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/0e4a9404ce14f395. Report an issue: GitHub.