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
- Verify the ExportRenderer/handler registration maps ImageButton (and only ImageButton) to ImageButtonRenderer.
- Confirm the element passed to the renderer is genuinely an ImageButton, not an Image, Button, or other control.
- 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
- Audit ExportRenderer attributes; ImageButtonRenderer must map only to ImageButton.
- Distinguish ImageButton from Image and Button in registrations.
- Add a renderer-resolution unit test asserting the correct element type for each registered renderer.
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
- Element is not of type CheckBox
- element must be of type RadioButton
- {nameof(element)} must be of type {typeof(IndicatorView).Nam
- {nameof(element)} must be of type {typeof(ItemsView).Name}
- {nameof(element)} must be of type {nameof(Button)}
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/a8e12b6a87a84601.
Report an issue: GitHub.