dotnet/maui · error · BuildException

XC0040

XC0040

Error message

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

What it means

XC0040 Conversion thrown by RectangleTypeConverter when the XAML string value is null or empty, before any parsing is attempted. This is the first guard in ConvertFromString for the Microsoft.Maui.Graphics.Rect type. The converter requires a non-empty, 4-part comma-separated string.

Source

Thrown at src/Controls/src/Build.Tasks/CompiledConverters/RectangleTypeConverter.cs:18

using System.Collections.Generic;
using System.Globalization;
using Microsoft.Maui.Controls.Build.Tasks;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Graphics;
using Mono.Cecil;
using Mono.Cecil.Cil;

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

			if (string.IsNullOrEmpty(value))
				throw new BuildException(BuildExceptionCode.Conversion, node, null, value, typeof(Rect));
			double x, y, w, h;
			var xywh = value.Split(',');
			if (xywh.Length != 4 ||
				!double.TryParse(xywh[0], NumberStyles.Number, CultureInfo.InvariantCulture, out x) ||
				!double.TryParse(xywh[1], NumberStyles.Number, CultureInfo.InvariantCulture, out y) ||
				!double.TryParse(xywh[2], NumberStyles.Number, CultureInfo.InvariantCulture, out w) ||
				!double.TryParse(xywh[3], NumberStyles.Number, CultureInfo.InvariantCulture, out h))
				throw new BuildException(BuildExceptionCode.Conversion, node, null, value, typeof(Rect));

			return GenerateIL(context, x, y, w, h, module);
		}

		IEnumerable<Instruction> GenerateIL(ILContext context, double x, double y, double w, double h, ModuleDefinition module)
		{
			//			IL_0000:  ldc.r8 3.1000000000000001
			//			IL_0009:  ldc.r8 4.2000000000000002
			//			IL_0012:  ldc.r8 5.2999999999999998
			//			IL_001b:  ldc.r8 6.4000000000000004

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Provide a non-empty value in 'x,y,width,height' format with exactly 4 comma-separated invariant-culture doubles.
  2. Remove the empty attribute if no value is needed.
  3. If using a binding, ensure it produces a non-empty string.

Example fix

<!-- before -->
<Clip>""</Clip>

<!-- after -->
<Clip>0,0,100,50</Clip>
Defensive patterns

Strategy: validation

Validate before calling

// Validate that a Rect XAML string is non-empty before building
static bool IsNonEmptyRectString(string value)
    => !string.IsNullOrEmpty(value);

Prevention

When it happens

Trigger: Setting a Rect-typed property (e.g. a clip region or bounds) to an empty string or a value that the XAML compiler sees as null/empty. This is the specific empty-check throw, distinct from the format/parsing throw at line 26.

Common situations: An empty attribute value, a binding that resolves to empty at compile time, or accidentally leaving the attribute as an empty string.

Related errors


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