dotnet/maui · error · BuildException

XC0040

XC0040

Error message

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

What it means

XC0040 Conversion thrown by RowDefinitionCollectionTypeConverter when the XAML string value is null or whitespace. The converter expects a comma-separated list of GridLength values (e.g. '*,Auto,100'). Note: if the value is non-empty but a segment is an invalid GridLength, the inner GridLengthTypeConverter throws its own XC0040 first; this throw at line 43 fires specifically for empty/whitespace input.

Source

Thrown at src/Controls/src/Build.Tasks/CompiledConverters/RowDefinitionCollectionTypeConverter.cs:43

				{
					yield return Create(Dup);
					yield return Create(Ldc_I4, i);
					foreach (var instruction in gridlengthconverter.ConvertFromString(parts[i], context, node))
						yield return instruction;
					yield return Create(Newobj, module.ImportCtorReference(
							context.Cache,
							type: ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls", "RowDefinition"),
							parameterTypes: new[] { ("Microsoft.Maui", "Microsoft.Maui", "GridLength") }));
					yield return Create(Stelem_Ref);
				}
				yield return Create(Newobj, module.ImportCtorReference(
						context.Cache,
						type: ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls", "RowDefinitionCollection"),
						paramCount: 1));
				yield break;

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

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Provide a non-empty, comma-separated list of valid GridLength values (e.g. '*,Auto,100').
  2. Remove trailing/leading commas that create empty segments.
  3. Ensure each segment uses valid GridLength syntax: 'Auto', '*', 'N*', or a plain number.

Example fix

<!-- before -->
<Grid.RowDefinitions></Grid.RowDefinitions>

<!-- after -->
<Grid.RowDefinitions>
  <RowDefinition Height="*" />
  <RowDefinition Height="Auto" />
</Grid.RowDefinitions>
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Setting RowDefinitions to an empty string or whitespace-only value. Each segment must be a valid GridLength ('auto', '*', 'N*', or a number); invalid segments throw from GridLengthTypeConverter before reaching this line.

Common situations: An empty RowDefinitions attribute, or trailing commas producing empty segments that the inner GridLength converter rejects. Forgetting to provide row definitions while the property is set to an empty string.

Related errors


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