dotnet/maui · error · BuildException

XC0040

XC0040

Error message

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

What it means

XamlC error XC0040 from ConstraintTypeConverter (Microsoft.Maui.Controls.Compatibility). This compiled converter supports only a single constant double, which it wraps in Constraint.Constant(size). An empty value or any string that is not a double throws XC0040. Relative/factor/multiply constraints are not supported by this string converter.

Source

Thrown at src/Controls/src/Build.Tasks/CompiledConverters/ConstraintTypeConverter.cs:20

using System.Globalization;
using Microsoft.Maui.Controls.Build.Tasks;
using Microsoft.Maui.Controls.Xaml;
using Mono.Cecil.Cil;
using static Mono.Cecil.Cil.Instruction;
using static Mono.Cecil.Cil.OpCodes;

namespace Microsoft.Maui.Controls.XamlC
{
	class ConstraintTypeConverter : ICompiledTypeConverter
	{
		public IEnumerable<Instruction> ConvertFromString(string value, ILContext context, BaseNode node)
		{
			var module = context.Body.Method.Module;

			double size;

			if (string.IsNullOrEmpty(value) || !double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out size))
				throw new BuildException(BuildExceptionCode.Conversion, node, null, value, typeof(Compatibility.Constraint));

			yield return Create(Ldc_R8, size);
			yield return Create(Call, module.ImportMethodReference(context.Cache, ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls.Compatibility", "Constraint"),
																   methodName: "Constant",
																   parameterTypes: new[] { ("mscorlib", "System", "Double") },
																   isStatic: true));
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Provide a single numeric constant (e.g. Constraint="42").
  2. For non-constant constraints (relative to parent/sibling, factor, multiply), use the ConstraintExpression markup extension instead of a plain string.

Example fix

<!-- before -->
<RelativeLayout>
  <BoxView RelativeLayout.WidthConstraint="" />
</RelativeLayout>

<!-- after -->
<RelativeLayout>
  <BoxView RelativeLayout.WidthConstraint="{ConstraintExpression Type=Constant, Constant=100}" />
</RelativeLayout>
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: A Constraint-typed attribute is set to an empty string or a non-numeric value; or an attempt is made to express a relative constraint (e.g. 'relativeToParent' style) as a plain string instead of via the ConstraintExpression markup extension.

Common situations: Leaving the attribute empty; typing a formula or unit suffix; confusing constant constraints with relative ones that require {ConstraintExpression ...}.

Related errors


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