{"record":{"id":"a8e12b6a87a84601","repo":"dotnet/maui","slug":"element-is-not-of-type-imagebutton","errorCode":null,"errorMessage":"Element is not of type ImageButton","messagePattern":"Element is not of type ImageButton","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Android/AppCompat/ImageButtonRenderer.cs","lineNumber":162,"sourceCode":"\t\t\tif (_disposed)\n\t\t\t{\n\t\t\t\treturn new SizeRequest();\n\t\t\t}\n\t\t\tMeasure(widthConstraint, heightConstraint);\n\t\t\treturn new SizeRequest(new Size(MeasuredWidth, MeasuredHeight), MinimumSize());\n\t\t}\n\n\t\tvoid IVisualElementRenderer.SetElement(VisualElement element)\n\t\t{\n\n\t\t\tif (element == null)\n\t\t\t{\n\t\t\t\tthrow new ArgumentNullException(nameof(element));\n\t\t\t}\n\n\t\t\tif (!(element is ImageButton image))\n\t\t\t{\n\t\t\t\tthrow new ArgumentException(\"Element is not of type \" + typeof(ImageButton), nameof(element));\n\t\t\t}\n\n\t\t\tImageButton oldElement = Element;\n\t\t\tElement = image;\n\n\t\t\tPerformance.Start(out string reference);\n\n\t\t\tif (oldElement != null)\n\t\t\t{\n\t\t\t\toldElement.PropertyChanged -= OnElementPropertyChanged;\n\t\t\t}\n\n\t\t\telement.PropertyChanged += OnElementPropertyChanged;\n\n\t\t\tif (_tracker == null)\n\t\t\t{\n\t\t\t\t_tracker = new VisualElementTracker(this);\n\t\t\t\tImageElementManager.Init(this);","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/AppCompat/ImageButtonRenderer.cs#L144-L180","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n[assembly: ExportRenderer(typeof(Image), typeof(ImageButtonRenderer))]\n\n// after\n[assembly: ExportRenderer(typeof(ImageButton), typeof(ImageButtonRenderer))]","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static bool IsImageButtonElement(VisualElement element) => element is ImageButton;","tryCatchPattern":null,"preventionTips":["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."],"tags":["android","renderer","imagebutton","type-mismatch","registration"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}