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

  1. Verify the ExportRenderer attribute's first type argument matches the generic TElement of the renderer.
  2. Make TElement the common base type if the renderer must serve multiple element subtypes.
  3. In custom renderers, prefer overriding SetElement(TElement) over re-implementing the interface to avoid bypassing the cast.
  4. 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

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


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/de745347fdb5b509. Report an issue: GitHub.