dotnet/maui · error · InvalidOperationException

Cannot convert "{0}" into {1}

Error message

Cannot convert "{0}" into {1}

What it means

FileImageSourceConverter.ConvertFrom throws InvalidOperationException when the incoming value is null (so strValue is null). The converter only knows how to build a FileImageSource from a non-null string via ImageSource.FromFile; a null input is treated as an unrecoverable conversion failure rather than producing an empty source.

Source

Thrown at src/Controls/src/Core/FileImageSourceConverter.cs:22

namespace Microsoft.Maui.Controls
{
	/// <summary>A <see cref="System.ComponentModel.TypeConverter"/> that converts to <see cref="Microsoft.Maui.Controls.FileImageSource"/>.</summary>
	public sealed class FileImageSourceConverter : TypeConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
			=> sourceType == typeof(string);

		public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
			=> destinationType == typeof(string);

		public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
		{
			var strValue = value?.ToString();
			if (strValue != null)
				return (FileImageSource)ImageSource.FromFile(strValue);

			throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", strValue, typeof(FileImageSource)));
		}

		public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
		{
			if (value is not FileImageSource fis)
				throw new NotSupportedException();
			return fis.File;
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Coalesce null before conversion: bind to a fallback path or use TargetNullValue in the Binding.
  2. Avoid invoking the converter directly with null; check the source value first.
  3. Use a default placeholder file path so strValue is never null.
  4. If you call ConvertFrom in code, guard `if (value is null) return null;` first.

Example fix

// before
var src = (FileImageSource)new FileImageSourceConverter().ConvertFrom(null);

// after
if (value is null) return null;
var src = (FileImageSource)new FileImageSourceConverter().ConvertFrom(value);
Defensive patterns

Strategy: validation

Validate before calling

if (value is null) return null; var strValue = value.ToString();

Type guard

static bool IsConvertible(object v) => v?.ToString() is not null;

Try / catch

try { return new FileImageSourceConverter().ConvertFrom(value); } catch (InvalidOperationException) { return null; }

Prevention

When it happens

Trigger: A XAML binding or TypeConverter path supplies null to a FileImageSource-typed property. Assigning ImageSource via a string converter when the bound model property is null and the converter is invoked (not short-circuited by the binding engine).

Common situations: Bound Image.Source to a nullable path whose value is initially null, a design-time preview with no data, or a converter invoked manually with null in tests.

Related errors


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