dotnet/maui · error · BuildException

XC0040

XC0040

Error message

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

What it means

XC0040 Conversion thrown by FontSizeTypeConverter when a XAML string is neither a valid invariant-culture double nor a valid NamedSize enum member (Default, Micro, Small, Medium, Large). The converter first attempts a numeric double parse, then falls back to the NamedSize enum. If both fail, the build-time exception is thrown with the target type 'double'.

Source

Thrown at src/Controls/src/Build.Tasks/CompiledConverters/FontSizeTypeConverter.cs:60

							context.Cache,
							("mscorlib", "System", "Type"),
							methodName: "GetTypeFromHandle",
							parameterTypes: [("mscorlib", "System", "RuntimeTypeHandle")],
							isStatic: true));
					}
					yield return Instruction.Create(OpCodes.Call, module.ImportMethodReference(
							context.Cache,
							("Microsoft.Maui.Controls", "Microsoft.Maui.Controls", "Device"),
							methodName: "GetNamedSize",
							parameterTypes: [("Microsoft.Maui.Controls", "Microsoft.Maui.Controls", "NamedSize"), ("System.Runtime", "System", "Type")],
							isStatic: true));

					yield break;
				}
#pragma warning restore CS0612 // Type or member is obsolete
			}

			throw new BuildException(BuildExceptionCode.Conversion, node, null, value, typeof(double));
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Provide a plain numeric double value for an explicit size, e.g. FontSize="14".
  2. Alternatively use a NamedSize enum value: Default, Micro, Small, Medium, or Large (though this API is deprecated).
  3. Remove any unit suffixes — the value must be a bare number or a NamedSize name.

Example fix

<!-- before -->
<Label FontSize="14pt" />

<!-- after -->
<Label FontSize="14" />
Defensive patterns

Strategy: validation

Validate before calling

// Validate a FontSize XAML string before building
static bool IsValidFontSize(string value)
{
    if (string.IsNullOrWhiteSpace(value)) return false;
    value = value.Trim();
    if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out _))
        return true;
    return Enum.TryParse<NamedSize>(value, out _);
}

Prevention

When it happens

Trigger: Setting a font-size-related property (e.g. Label.FontSize) to a string like 'Big', '12pt', '1.5em', or any non-numeric, non-NamedSize value. The NamedSize fallback is obsolete/legacy.

Common situations: Using CSS or web-style font size units (pt, px, em, rem) which are not recognized. Using a NamedSize value that does not exist. Relying on NamedSize which is deprecated in newer MAUI versions.

Related errors


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