dotnet/maui · error · ArgumentException
Element is not of type " + typeof(Image)
Error message
Element is not of type " + typeof(Image)
What it means
ImageRenderer.SetElement explicitly rejects null with ArgumentNullException, then uses `as Image` and throws ArgumentException if the element is not an Image. The message is built by string concatenation (`"Element is not of type " + typeof(Image)`), so the message text is 'Element is not of type Microsoft.Maui.Controls.Image'.
Source
Thrown at src/Compatibility/Core/src/Android/FastRenderers/ImageRenderer.cs:126
SizeRequest IVisualElementRenderer.GetDesiredSize(int widthConstraint, int heightConstraint)
{
if (_disposed)
{
return new SizeRequest();
}
Measure(widthConstraint, heightConstraint);
return new SizeRequest(new Size(MeasuredWidth, MeasuredHeight), MinimumSize());
}
void IVisualElementRenderer.SetElement(VisualElement element)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
var image = element as Image;
if (image == null)
throw new ArgumentException("Element is not of type " + typeof(Image), nameof(element));
Image oldElement = _element;
_element = image;
Performance.Start(out string reference);
if (oldElement != null)
oldElement.PropertyChanged -= OnElementPropertyChanged;
element.PropertyChanged += OnElementPropertyChanged;
if (_visualElementTracker == null)
{
_visualElementTracker = new VisualElementTracker(this);
}
if (_visualElementRenderer == null)
{View on GitHub (pinned to f377ff1c5e)
Solutions
- Verify ExportRenderer(typeof(Image), typeof(ImageRenderer)) (or subclass) matches.
- Do not reuse ImageRenderer for unrelated controls.
- If supporting a derived Image type, ensure the type genuinely inherits Image so `is Image` succeeds.
Example fix
// before [assembly: ExportRenderer(typeof(ImageButton), typeof(CustomImageRenderer))] // after [assembly: ExportRenderer(typeof(Image), typeof(CustomImageRenderer))]
Defensive patterns
Strategy: validation
Validate before calling
if (element is null) throw new ArgumentNullException(nameof(element));
if (element is not Image) throw new ArgumentException("Expected Image.", nameof(element));
((IVisualElementRenderer)renderer).SetElement(element); Type guard
static bool IsImage(VisualElement e) => e is Image;
Prevention
- Confirm ExportRenderer(typeof(Image), ...).
- Don't route ImageButton or other image-like controls to ImageRenderer.
- Null-check externally since the message does not distinguish null.
When it happens
Trigger: SetElement called with a non-Image VisualElement. Commonly a mis-registered ExportRenderer attribute or a manual renderer reuse.
Common situations: Custom ImageRenderer subclass paired with a non-Image control in the ExportRenderer attribute; migration from a different image control without updating the registration; handler dispatch routing the wrong renderer.
Related errors
- {nameof(element)} must be of type {nameof(Button)}
- Element must be of type Frame
- Element must be of type Label
- {nameof(element)} must be of type {typeof(IndicatorView).Nam
- {nameof(element)} must be of type {typeof(ItemsView).Name}
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/79a37c5a09222e14.
Report an issue: GitHub.