dotnet/maui · error · ArgumentException
Element is not of type {typeof(TElement)}
Error message
Element is not of type {typeof(TElement)} What it means
The explicit-interface implementation IVisualElementRenderer.SetElement(VisualElement) on Tizen's VisualElementRenderer<TElement> throws ArgumentException when the incoming VisualElement cannot be cast to the renderer's generic TElement type. The renderer is registered against one element type but the framework routed a different subtype to it.
Source
Thrown at src/Compatibility/Core/src/Tizen/Renderers/VisualElementRenderer.cs:211
newElement.IsPlatformEnabled = true;
OnElementReady();
SendVisualElementInitialized(newElement, NativeView);
}
void IVisualElementRenderer.UpdateLayout()
{
UpdateLayout();
}
void IVisualElementRenderer.SetElement(VisualElement element)
{
TElement tElement = element as TElement;
if (tElement == null)
{
throw new ArgumentException("Element is not of type " + typeof(TElement), nameof(element));
}
SetElement(tElement);
}
/// <summary>
/// Registers the effect with the element by establishing the parent-child relations needed for rendering on the specific platform.
/// </summary>
/// <param name="effect">The effect to register.</param>
void IEffectControlProvider.RegisterEffect(Effect effect)
{
RegisterEffect(effect);
}
/// <summary>
/// Registers the effect with the element by establishing the parent-child relations needed for rendering on the specific platform.
/// </summary>
/// <param name="effect">The effect to register.</param>
protected void RegisterEffect(Effect effect)View on GitHub (pinned to f377ff1c5e)
Solutions
- Verify the ExportRenderer attribute's first type argument matches the generic TElement of the renderer.
- Make TElement the common base type if the renderer must serve multiple element subtypes.
- In custom renderers, prefer overriding SetElement(TElement) over re-implementing the interface to avoid bypassing the cast.
- Check that DataTemplates / page factories produce elements assignable to the registered type.
Example fix
// before [assembly: ExportRenderer(typeof(Button), typeof(CustomButtonRenderer<MyCustomButton>))] // after [assembly: ExportRenderer(typeof(MyCustomButton), typeof(CustomButtonRenderer<MyCustomButton>))]
Defensive patterns
Strategy: type-guard
Validate before calling
if (element is TElement typed) renderer.SetElement(typed);
else throw new InvalidOperationException($"Expected {typeof(TElement)}, got {element?.GetType()}"); Type guard
static bool IsExpectedElement(VisualElement e) => e is TElement;
Try / catch
try { ((IVisualElementRenderer)renderer).SetElement(element); }
catch (ArgumentException ex) when (ex.Message.Contains("not of type"))
{ /* registrar mismatch; log element type and renderer generic */ } Prevention
- Keep ExportRenderer attribute type argument aligned with the renderer's generic parameter.
- Add a unit test asserting the registrar returns the expected renderer type for each element.
- Avoid sharing one renderer class across unrelated element types.
When it happens
Trigger: The Tizen renderer registrar resolves this renderer for a VisualElement that is not assignable to TElement; a custom renderer is registered with the wrong ExportRenderer attribute or wrong generic parameter; DataTemplate returns a mismatched element type.
Common situations: Registering CustomButtonRenderer : VisualElementRenderer<MyCustomButton> against a Button export; switching an element's class without updating the renderer registration; shared renderer reused for sibling element types.
Related errors
- Element is not of type CheckBox
- Element is not of type ImageButton
- element must be of type RadioButton
- {nameof(element)} must be of type {typeof(IndicatorView).Nam
- {nameof(element)} must be of type {typeof(ItemsView).Name}
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/de745347fdb5b509.
Report an issue: GitHub.