{"record":{"id":"d2ce660c03f8f82f","repo":"dotnet/maui","slug":"nameof-element-must-be-of-type-nameof-button","errorCode":null,"errorMessage":"{nameof(element)} must be of type {nameof(Button)}","messagePattern":"(.+?) must be of type (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Android/FastRenderers/ButtonRenderer.cs","lineNumber":114,"sourceCode":"\n\t\t\tvar result = _buttonLayoutManager.GetDesiredSize(widthConstraint, heightConstraint);\n\n\t\t\tif (setHint)\n\t\t\t\tControl.Hint = hint;\n\n\t\t\treturn result;\n\t\t}\n\n\t\tvoid IVisualElementRenderer.SetElement(VisualElement element)\n\t\t{\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 Button))\n\t\t\t{\n\t\t\t\tthrow new ArgumentException($\"{nameof(element)} must be of type {nameof(Button)}\");\n\t\t\t}\n\n\t\t\tVisualElement oldElement = Button;\n\t\t\tButton = (Button)element;\n\n\t\t\tPerformance.Start(out string reference);\n\n\t\t\tOnElementChanged(new ElementChangedEventArgs<Button>(oldElement as Button, Button));\n\n\t\t\tSendVisualElementInitialized(element, this);\n\n\t\t\tPerformance.Stop(reference);\n\t\t}\n\n\t\tvoid IVisualElementRenderer.SetLabelFor(int? id)\n\t\t{\n\t\t\tif (_defaultLabelFor == null)\n\t\t\t{","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/FastRenderers/ButtonRenderer.cs#L96-L132","documentation":"The fast ButtonRenderer's explicit SetElement requires a Button. It null-checks then verifies `element is Button` before casting. The fast-renderer family (Button, Label, Image, Frame) each enforce their concrete type because they are not generic.","triggerScenarios":"SetElement called with a VisualElement that is not a Button — almost always a wrong ExportRenderer registration or a manual renderer call in custom handler code.","commonSituations":"A custom subclass of ButtonRenderer registered for a non-Button control, or a handler migration where the old fast-renderer attribute points at the wrong control type.","solutions":["Ensure ExportRenderer(typeof(Button), typeof(ButtonRenderer)) (or your subclass) is correctly typed.","Do not pass non-Button elements when reusing or manually driving the renderer.","If subclassing for a control that extends Button, the check still passes; otherwise pick the right base renderer."],"exampleFix":"// before\n[assembly: ExportRenderer(typeof(Label), typeof(MyButtonRenderer))]\n// after\n[assembly: ExportRenderer(typeof(Button), typeof(MyButtonRenderer))]","handlingStrategy":"type-guard","validationCode":"if (element is null) throw new ArgumentNullException(nameof(element));\nif (element is not Button) throw new ArgumentException(\"Expected Button.\", nameof(element));\n((IVisualElementRenderer)renderer).SetElement(element);","typeGuard":"static bool IsButton(VisualElement e) => e is Button;","tryCatchPattern":null,"preventionTips":["Keep ExportRenderer(typeof(Button), ...) attributes accurate.","Don't reuse fast renderers across control types.","Validate before any manual renderer attachment."],"tags":["android","renderer","type-mismatch","button","fast-renderer","maui-compat"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}