dotnet/maui · error · BuildException

XC0040

XC0040

Error message

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

What it means

XC0040 Conversion thrown by FlexBasisTypeConverter when a XAML string does not represent a valid Microsoft.Maui.Layouts.FlexBasis. The converter accepts the literal 'Auto', a percentage string like '50%' (parsed as a relative basis), or a plain invariant-culture float. If none of these match, the build exception fires.

Source

Thrown at src/Controls/src/Build.Tasks/CompiledConverters/FlexBasisTypeConverter.cs:47

				{
					yield return Instruction.Create(OpCodes.Ldc_R4, (float)(relflex / 100));
					yield return Instruction.Create(OpCodes.Ldc_I4_1); //isRelative: true
					yield return Instruction.Create(OpCodes.Newobj, module.ImportCtorReference(context.Cache, ("Microsoft.Maui", "Microsoft.Maui.Layouts", "FlexBasis"), parameterTypes: new[] {
						("mscorlib", "System", "Single"),
						("mscorlib", "System", "Boolean")}));
					yield break;
				}
				if (float.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out float flex))
				{
					yield return Instruction.Create(OpCodes.Ldc_R4, flex);
					yield return Instruction.Create(OpCodes.Ldc_I4_0); //isRelative: false
					yield return Instruction.Create(OpCodes.Newobj, module.ImportCtorReference(context.Cache, ("Microsoft.Maui", "Microsoft.Maui.Layouts", "FlexBasis"), parameterTypes: new[] {
						("mscorlib", "System", "Single"),
						("mscorlib", "System", "Boolean")}));
					yield break;
				}
			}
			throw new BuildException(BuildExceptionCode.Conversion, node, null, value, typeof(FlexBasis));
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Use 'Auto' for automatic sizing, a plain number (e.g. '100') for absolute length, or 'NN%' for a percentage of available space.
  2. Ensure percentage values have the '%' suffix and contain a valid float before it.
  3. Remove any unit suffixes other than '%' — pixels, rem, etc. are not supported.

Example fix

<!-- before -->
<FlexLayout.Basis>50</FlexLayout.Basis>

<!-- after -->
<FlexLayout.Basis>50%</FlexLayout.Basis>
<!-- or -->
<FlexLayout.Basis>Auto</FlexLayout.Basis>
Defensive patterns

Strategy: validation

Validate before calling

// Validate a FlexBasis XAML string before building
static bool IsValidFlexBasis(string value)
{
    if (string.IsNullOrWhiteSpace(value)) return false;
    value = value.Trim();
    if (value == "Auto") return true;
    if (value.EndsWith('%') &&
        float.TryParse(value[..^1], NumberStyles.Number, CultureInfo.InvariantCulture, out _))
        return true;
    return float.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out _);
}

Prevention

When it happens

Trigger: Setting FlexLayout.Basis to an unrecognized format such as 'auto ' (trailing issue is fine after trim, but 'Automatic' fails), '0.5px', or a non-numeric word. A plain '100' (absolute) or '50%' (relative) or 'Auto' are the only accepted forms.

Common situations: Confusing absolute (unitless number) with percentage — forgetting the '%' suffix. Using CSS-style 'flex-basis' values like '1rem' or '100px'. Misspelling 'Auto' as 'auto' works (case-insensitive for the percent check but 'Auto' is checked with exact equality, so use 'Auto').

Related errors


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