{"record":{"id":"38c7eb3004ba599d","repo":"dotnet/maui","slug":"element-is-not-of-type-checkbox","errorCode":null,"errorMessage":"Element is not of type CheckBox","messagePattern":"Element is not of type CheckBox","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Android/AppCompat/CheckBoxRendererBase.cs","lineNumber":107,"sourceCode":"\t\t{\n\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\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 CheckBox checkBox))\n\t\t\t{\n\t\t\t\tthrow new ArgumentException(\"Element is not of type \" + typeof(CheckBox), nameof(element));\n\t\t\t}\n\n\t\t\tCheckBox oldElement = Element;\n\t\t\tElement = checkBox;\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}","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/AppCompat/CheckBoxRendererBase.cs#L89-L125","documentation":"Thrown by CheckBoxRendererBase.SetElement when the VisualElement passed to the renderer is not a CheckBox. The AppCompat renderer for CheckBox is hard-bound to the CheckBox controls type; any other element type is a registration or wiring mistake. The guard runs before any property subscription so the renderer never enters an invalid state.","triggerScenarios":"A custom handler/ registrar maps the wrong renderer to CheckBox, or a custom VisualElementRenderer is manually fed a non-CheckBox element. Also occurs when a custom control subclasses a different type but is erroneously registered against CheckBox in the platform registrar.","commonSituations":"Misconfiguring ExportRenderer attributes (e.g., [assembly: ExportRenderer(typeof(MySwitch), typeof(CheckBoxRenderer))]). Version mismatches where a control assembly exports a renderer for a type that was renamed or moved. Manually invoking SetElement with the wrong element instance in test harnesses.","solutions":["Verify the ExportRenderer/ handler registration maps CheckBox (and only CheckBox) to CheckBoxRendererBase-derived types.","Check that the element passed to the renderer is actually a CheckBox instance and not a subclass of a different control.","If you need a custom checkbox-like control, subclass CheckBox or register a dedicated renderer for your custom type rather than reusing CheckBoxRendererBase."],"exampleFix":"// before\n[assembly: ExportRenderer(typeof(MyToggle), typeof(CheckBoxRenderer))]\n\n// after\n[assembly: ExportRenderer(typeof(CheckBox), typeof(CheckBoxRenderer))]\n[assembly: ExportRenderer(typeof(MyToggle), typeof(MyToggleRenderer))]","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static bool IsCheckBoxElement(VisualElement element) => element is CheckBox;","tryCatchPattern":null,"preventionTips":["Audit all [assembly: ExportRenderer(...)] attributes in the solution and ensure each maps the correct control type to its renderer.","Add a unit test that resolves the renderer for CheckBox and asserts the returned element type matches.","When subclassing controls, register a dedicated renderer for the subclass rather than reusing CheckBox's."],"tags":["android","renderer","checkbox","type-mismatch","registration"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}