dotnet/maui · error · BuildException

XC0040

XC0040

Error message

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

What it means

XC0040 Conversion thrown by ThicknessTypeConverter when a XAML string cannot be parsed into a Microsoft.Maui.Thickness. The converter splits on commas and accepts 1 value (uniform), 2 values (horizontal, vertical), or 4 values (left, top, right, bottom). If the count is not 1, 2, or 4, or any segment fails double.TryParse, the build exception fires.

Source

Thrown at src/Controls/src/Build.Tasks/CompiledConverters/ThicknessTypeConverter.cs:41

					case 1:
						if (double.TryParse(thickness[0], NumberStyles.Number, CultureInfo.InvariantCulture, out l))
							return GenerateIL(context, module, l);
						break;
					case 2:
						if (double.TryParse(thickness[0], NumberStyles.Number, CultureInfo.InvariantCulture, out l) &&
							double.TryParse(thickness[1], NumberStyles.Number, CultureInfo.InvariantCulture, out t))
							return GenerateIL(context, module, l, t);
						break;
					case 4:
						if (double.TryParse(thickness[0], NumberStyles.Number, CultureInfo.InvariantCulture, out l) &&
							double.TryParse(thickness[1], NumberStyles.Number, CultureInfo.InvariantCulture, out t) &&
							double.TryParse(thickness[2], NumberStyles.Number, CultureInfo.InvariantCulture, out r) &&
							double.TryParse(thickness[3], NumberStyles.Number, CultureInfo.InvariantCulture, out b))
							return GenerateIL(context, module, l, t, r, b);
						break;
				}
			}
			throw new BuildException(BuildExceptionCode.Conversion, node, null, value, typeof(Thickness));
		}

		IEnumerable<Instruction> GenerateIL(ILContext context, ModuleDefinition module, params double[] args)
		{
			foreach (var d in args)
				yield return Instruction.Create(OpCodes.Ldc_R8, d);
			yield return Instruction.Create(OpCodes.Newobj, module.ImportCtorReference(context.Cache, ("Microsoft.Maui", "Microsoft.Maui", "Thickness"), parameterTypes: args.Select(a => ("mscorlib", "System", "Double")).ToArray()));
		}
	}

}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Use 1 value (uniform), 2 values (horizontal, vertical), or 4 values (left, top, right, bottom), comma-separated.
  2. Remove any unit suffixes — values must be bare invariant-culture doubles.
  3. For 3-sided padding, use 4 values with 0 for the side you want to be zero (e.g. '10,0,10,0' for left/right only).

Example fix

<!-- before -->
<Label Margin="10,20,30" />

<!-- after -->
<Label Margin="10,20,30,40" />
Defensive patterns

Strategy: validation

Validate before calling

// Validate a Thickness XAML string (1, 2, or 4 doubles) before building
static bool IsValidThickness(string value)
{
    if (string.IsNullOrEmpty(value)) return false;
    var parts = value.Split(',');
    if (parts.Length != 1 && parts.Length != 2 && parts.Length != 4) return false;
    return parts.All(p => double.TryParse(p.Trim(), NumberStyles.Number, CultureInfo.InvariantCulture, out _));
}

Prevention

When it happens

Trigger: Setting Margin, Padding, or another Thickness property to 3 comma-separated values (unsupported), a non-numeric segment, or empty. Examples: '10,20,30' (3 values — Thickness does not support 3), '10px,20px' (unit suffixes), '1,2,3,4,5' (5 values).

Common situations: Providing 3 values ( Thickness supports only 1, 2, or 4). Using unit suffixes like 'px'. Mixing up the order (Thickness order is left, top, right, bottom, not top/right/bottom/left).

Related errors


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