dotnet/maui · error · NotSupportedException
throw new NotSupportedException();
Error message
throw new NotSupportedException();
What it means
FileImageSourceConverter.ConvertTo throws a bare NotSupportedException (no message) when the value passed in is not a FileImageSource. The converter only serializes a FileImageSource back to its File string; any other type is unsupported. The lack of a message makes diagnostics harder.
Source
Thrown at src/Controls/src/Core/FileImageSourceConverter.cs:28
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
- Route conversion through the generic ImageSourceConverter or ImageTypeConverter that handles all ImageSource kinds.
- Type-check before calling: `if (value is FileImageSource fis) ... else use appropriate converter`.
- Avoid ConvertTo for design-time serialization of polymorphic ImageSource values.
Example fix
// before
var path = new FileImageSourceConverter().ConvertTo(myUriImageSource, typeof(string));
// after
if (value is FileImageSource fis)
return fis.File;
throw new NotSupportedException($"Expected FileImageSource, got {value?.GetType()}."); Defensive patterns
Strategy: type-guard
Validate before calling
if (value is not FileImageSource) throw new NotSupportedException();
Type guard
static bool IsFileImageSource(object v) => v is FileImageSource;
Try / catch
try { return converter.ConvertTo(value, typeof(string)); } catch (NotSupportedException) { /* route to correct ImageSource converter */ } Prevention
- Type-check before invoking ConvertTo on polymorphic ImageSource values.
- Use the generic ImageTypeConverter/ImageSourceConverter for mixed kinds.
- Avoid serializer round-trips through the file-specific converter.
When it happens
Trigger: Calling ConvertTo with a StreamImageSource, FontImageSource, UriImageSource, or a non-ImageSource object. XAML round-tripping or serialization pipelines that route mixed ImageSource kinds through one converter.
Common situations: Switching an Image source from a file to a URI/font image while a converter-based serializer or property grid still routes through FileImageSourceConverter.
Related errors
- Cannot convert "{0}" into {1}
- throw new NotSupportedException();
- XC0040
- Cannot convert "{strValue}" into {typeof(FlowDirection)}
- No test assembly found.
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/f4e06e73fdd87a42.
Report an issue: GitHub.