{"record":{"id":"df2a0a329abcbe2d","repo":"dotnet/maui","slug":"element-must-be-of-type-button","errorCode":null,"errorMessage":"Element must be of type Button.","messagePattern":"Element must be of type Button\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Material/src/Android/MaterialButtonRenderer.cs","lineNumber":418,"sourceCode":"\t\tAColor IBorderVisualElementRenderer.ShadowColor => ShadowColor;\n\t\tbool IBorderVisualElementRenderer.UseDefaultPadding() => OnThisPlatform().UseDefaultPadding();\n\t\tbool IBorderVisualElementRenderer.UseDefaultShadow() => OnThisPlatform().UseDefaultShadow();\n\t\tbool IBorderVisualElementRenderer.IsShadowEnabled() => true;\n\t\tVisualElement IBorderVisualElementRenderer.Element => Element;\n\t\tAView IBorderVisualElementRenderer.View => this;\n\n\t\t// IVisualElementRenderer\n\t\tVisualElement IVisualElementRenderer.Element => Element;\n\t\tVisualElementTracker IVisualElementRenderer.Tracker => _tracker;\n\t\tAView IVisualElementRenderer.View => this;\n\n\t\tSizeRequest IVisualElementRenderer.GetDesiredSize(int widthConstraint, int heightConstraint)\n\t\t{\n\t\t\treturn _buttonLayoutManager.GetDesiredSize(widthConstraint, heightConstraint);\n\t\t}\n\n\t\tvoid IVisualElementRenderer.SetElement(VisualElement element) =>\n\t\t\tElement = (element as Button) ?? throw new ArgumentException(\"Element must be of type Button.\");\n\n\t\tvoid IVisualElementRenderer.SetLabelFor(int? id)\n\t\t{\n\t\t\tif (_defaultLabelFor == null)\n\t\t\t\t_defaultLabelFor = ViewCompat.GetLabelFor(this);\n\t\t\tViewCompat.SetLabelFor(this, (int)(id ?? _defaultLabelFor));\n\t\t}\n\n\t\tvoid IVisualElementRenderer.UpdateLayout() =>\n\t\t\t_tracker?.UpdateLayout();\n\n\t\t// IViewRenderer\n\t\tvoid IViewRenderer.MeasureExactly() =>\n\t\t\tViewRenderer.MeasureExactly(this, Element, Context);\n\n\t\t// ITabStop\n\t\tAView ITabStop.TabStop => this;\n","sourceCodeStart":400,"sourceCodeEnd":436,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Material/src/Android/MaterialButtonRenderer.cs#L400-L436","documentation":"MaterialButtonRenderer renders Material Design Buttons on Android. SetElement casts the incoming VisualElement to Button and throws ArgumentException if the element is not a Button. This is a hard contract: the renderer's layout, styling, and event wiring all assume a Button element.","triggerScenarios":"The renderer registry dispatches a non-Button element to MaterialButtonRenderer. Occurs with incorrect ExportRenderer mappings or when a custom element type not deriving from Button is paired with this renderer.","commonSituations":"Misconfigured ExportRenderer attributes pairing a non-Button element with MaterialButtonRenderer. Stale registrations left after refactoring controls. Third-party libraries registering conflicting Button renderers.","solutions":["Verify ExportRenderer maps Button (not a custom type) to MaterialButtonRenderer.","Ensure custom controls intended for this renderer derive from Button.","Audit the renderer registrar for duplicate Button registrations across assemblies.","Confirm `VisualMarker.Material` is applied to actual Button instances."],"exampleFix":"// before\n[assembly: ExportRenderer(typeof(MyCustomLabel), typeof(MaterialButtonRenderer))]\n\n// after\n[assembly: ExportRenderer(typeof(Button), typeof(MaterialButtonRenderer))]","handlingStrategy":"type-guard","validationCode":"if (element is not Button)\n    throw new InvalidOperationException($\"Expected Button, got {element?.GetType().Name}\");\nrenderer.SetElement(element);","typeGuard":"static bool IsButton(VisualElement? element) => element is Button;","tryCatchPattern":null,"preventionTips":["Verify ExportRenderer maps Button to MaterialButtonRenderer in every platform project.","Ensure custom button controls inherit from Button.","Remove stale renderer registrations after refactoring element hierarchies."],"tags":["maui","android","material","renderer","type-mismatch","button"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}