dotnet/maui · error · ArgumentException

Element is not of type ImageButton

Error message

Element is not of type ImageButton

What it means

Thrown by ImageButtonRenderer.SetElement when the VisualElement passed to the renderer is not an ImageButton. The AppCompat ImageButton renderer is exclusively bound to the ImageButton control type; receiving any other element indicates a misregistered handler or incorrect manual element assignment.

Source

Thrown at src/Compatibility/Core/src/Android/AppCompat/ImageButtonRenderer.cs:162

			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));
			}

			if (!(element is ImageButton image))
			{
				throw new ArgumentException("Element is not of type " + typeof(ImageButton), nameof(element));
			}

			ImageButton oldElement = Element;
			Element = image;

			Performance.Start(out string reference);

			if (oldElement != null)
			{
				oldElement.PropertyChanged -= OnElementPropertyChanged;
			}

			element.PropertyChanged += OnElementPropertyChanged;

			if (_tracker == null)
			{
				_tracker = new VisualElementTracker(this);
				ImageElementManager.Init(this);

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Verify the ExportRenderer/handler registration maps ImageButton (and only ImageButton) to ImageButtonRenderer.
  2. Confirm the element passed to the renderer is genuinely an ImageButton, not an Image, Button, or other control.
  3. For custom image-button controls, register a dedicated renderer for your custom type instead of reusing ImageButtonRenderer.

Example fix

// before
[assembly: ExportRenderer(typeof(Image), typeof(ImageButtonRenderer))]

// after
[assembly: ExportRenderer(typeof(ImageButton), typeof(ImageButtonRenderer))]
Defensive patterns

Strategy: type-guard

Type guard

static bool IsImageButtonElement(VisualElement element) => element is ImageButton;

Prevention

When it happens

Trigger: An ExportRenderer attribute maps a non-ImageButton type to ImageButtonRenderer, or a custom renderer/handler is manually fed the wrong element. Also occurs when an assembly exports a renderer for a control whose type was renamed or replaced across versions.

Common situations: Mis-typed [assembly: ExportRenderer(typeof(Image), typeof(ImageButtonRenderer))] (confusing Image and ImageButton). Version upgrades where ImageButton moved namespaces. Test harnesses calling SetElement with a stub element.

Related errors


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