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

  1. Verify ExportRenderer(typeof(Image), typeof(ImageRenderer)) (or subclass) matches.
  2. Do not reuse ImageRenderer for unrelated controls.
  3. 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

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


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