dotnet/maui · error · BuildException

XC0040

XC0040

Error message

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

What it means

XC0040 Conversion thrown by GridLengthTypeConverter when a XAML string is not a valid Microsoft.Maui.GridLength. Accepted forms are: 'auto'/'Auto' (case-insensitive), '*' (one star), 'N*' (N stars with a numeric multiplier), or a plain invariant-culture double (absolute pixels). If the value is null/whitespace or none of these patterns match, the build exception fires.

Source

Thrown at src/Controls/src/Build.Tasks/CompiledConverters/GridLengthTypeConverter.cs:52

					yield return Create(Ldc_R8, length);
					yield return Create(Ldc_I4, (int)GridUnitType.Star);
					yield return Create(Newobj, module.ImportCtorReference(context.Cache,
							type: module.GetTypeDefinition(context.Cache, ("Microsoft.Maui", "Microsoft.Maui", "GridLength")),
							parameterTypes: new[] { module.TypeSystem.Double, module.GetTypeDefinition(context.Cache, ("Microsoft.Maui", "Microsoft.Maui", "GridUnitType")) }));
					yield break;
				}
				if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out length))
				{
					yield return Create(Ldc_R8, length);
					yield return Create(Newobj, module.ImportCtorReference(context.Cache,
							type: module.GetTypeDefinition(context.Cache, ("Microsoft.Maui", "Microsoft.Maui", "GridLength")),
							parameterTypes: new[] { module.TypeSystem.Double }));
					yield break;
				}

			}

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

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Use 'Auto' for auto-sized rows/columns, '*' for proportional (star) sizing, 'N*' for weighted proportional, or a plain number for absolute pixels.
  2. Check for trailing characters, stray unit suffixes, or typos in 'auto' and '*'.
  3. For star sizing, ensure the numeric prefix before '*' is a valid invariant-culture double.

Example fix

<!-- before -->
<RowDefinitions>100px,2x,auto</RowDefinitions>

<!-- after -->
<RowDefinitions>100,2*,Auto</RowDefinitions>
Defensive patterns

Strategy: validation

Validate before calling

// Validate a GridLength XAML string before building
static bool IsValidGridLength(string value)
{
    if (string.IsNullOrWhiteSpace(value)) return false;
    value = value.Trim();
    if (value.Equals("auto", StringComparison.OrdinalIgnoreCase)) return true;
    if (value == "*") return true;
    if (value.EndsWith('*') &&
        double.TryParse(value[..^1], NumberStyles.Number, CultureInfo.InvariantCulture, out _))
        return true;
    return double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out _);
}

Prevention

When it happens

Trigger: Setting a Grid row/column definition to an invalid format such as 'star', '2x', 'auto ', '100px', or a non-numeric word. Valid examples: 'auto', '*', '2*', '100'.

Common situations: Misspelling 'auto' (e.g. 'Auto' works but 'autom' does not). Using '2x' instead of '2*'. Adding 'px' or other unit suffixes. An empty value in a RowDefinitions/ColumnDefinitions string.

Related errors


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