dotnet/maui · error · BuildException

XC0040

XC0040

Error message

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

What it means

XC0040 Conversion thrown by ImageSourceTypeConverter when the XAML string value is null or empty. The converter accepts any non-empty, non-whitespace string: if it parses as an absolute non-file URI, it generates ImageSource.FromUri; otherwise it generates ImageSource.FromFile. The only failure condition is a null or empty value.

Source

Thrown at src/Controls/src/Build.Tasks/CompiledConverters/ImageSourceTypeConverter.cs:36

			if (Uri.TryCreate(value, UriKind.Absolute, out Uri uri) && uri.Scheme != "file")
			{
				yield return Instruction.Create(OpCodes.Ldstr, value);
				yield return Instruction.Create(OpCodes.Ldc_I4_1); // (int)UriKind.Absolute is 1
				yield return Instruction.Create(OpCodes.Newobj, module.ImportCtorReference(context.Cache, ("System", "System", "Uri"), parameterTypes: new[] {
																							("mscorlib", "System", "String"),
																							("System", "System", "UriKind")}));
				yield return Instruction.Create(OpCodes.Call, module.ImportMethodReference(context.Cache, ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls", "ImageSource"), "FromUri", parameterTypes: new[] {
																							("System", "System", "Uri")}, isStatic: true));
			}
			else
			{
				yield return Instruction.Create(OpCodes.Ldstr, value);
				yield return Instruction.Create(OpCodes.Call, module.ImportMethodReference(context.Cache, ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls", "ImageSource"), "FromFile", parameterTypes: new[] {
																							("mscorlib", "System", "String")}, isStatic: true));
			}
			yield break;
		}
		throw new BuildException(BuildExceptionCode.Conversion, node, null, value, typeof(ImageSource));
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Provide a non-empty URI string (e.g. 'https://example.com/image.png') or a file/resource name.
  2. Remove the empty attribute entirely if no image source is intended.
  3. If the value comes from a binding, ensure it resolves to a non-empty string at compile time.

Example fix

<!-- before -->
<Image Source="" />

<!-- after -->
<Image Source="myimage.png" />
Defensive patterns

Strategy: validation

Validate before calling

// Validate an ImageSource XAML string — any non-empty value is accepted
static bool IsValidImageSource(string value)
    => !string.IsNullOrWhiteSpace(value);

Prevention

When it happens

Trigger: Setting an ImageSource property to an empty string or a binding/expression that evaluates to empty at XAML compile time. This is rare because most empty bindings are handled before the converter runs, but a literal empty string or whitespace-only attribute triggers it.

Common situations: An empty Source attribute on an Image, or a dynamically constructed value that the compiler sees as empty. Accidentally leaving Source="" in markup.

Related errors


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