Using XF renderer in .NET MAUI not possible -> System.InvalidCastException: 'Specified cast is not valid.'

See original GitHub issue

Description

Its not possible to port an existing Xamarin.Forms custom control Android renderer to MAUI. During runtime an “System.InvalidCastException: ‘Specified cast is not valid.’” is thrown.

Custom control:

    public class CardsView : AbsoluteLayout
    {
        public CardsView()
        {
            Background = Colors.Red;
        }
    }

Renderer:

public class CardsViewRenderer : VisualElementRenderer<CardsView> 
{
        public CardsViewRenderer(Context context) : base(context)
        {
        }
    }

MauiProgram:

.UseMauiCompatibility()
.ConfigureMauiHandlers(handlers =>
{
    handlers.AddCompatibilityRenderer(typeof(CardsView), typeof(CardsViewRenderer));
})

Steps to Reproduce

1.) Fetch repo 2.) Run the solution on Android emulator 3.) Exception thrown

Link to public reproduction project repository

https://github.com/maexsp/RendererDemo/tree/master

Version with bug

6.0.312

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

Android 10, API 29

Did you find any workaround?

No.

Relevant log output

0xFFFFFFFFFFFFFFFF in Android.Runtime.JNIEnv.monodroid_debugger_unhandled_exception	C#
 	0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5	C#
 	0x23 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:353,26	C#
 	0x67 in Microsoft.Maui.Controls.Internals.Registrar<Microsoft.Maui.Controls.IRegisterable>.GetHandler at D:\a\_work\1\s\src\Controls\src\Core\Registrar.cs:106,6	C#
 	0x42 in Microsoft.Maui.Controls.Internals.Registrar<Microsoft.Maui.Controls.IRegisterable>.GetHandlerForObject<Microsoft.Maui.Controls.Compatibility.Platform.Android.IVisualElementRenderer> at D:\a\_work\1\s\src\Controls\src\Core\Registrar.cs:145,4	C#
 	0x1D in Microsoft.Maui.Controls.Compatibility.RendererToHandlerShim.CreateRenderer at D:\a\_work\1\s\src\Compatibility\Core\src\RendererToHandlerShim.Android.cs:26,5	C#
 	0xB in Microsoft.Maui.Controls.Compatibility.RendererToHandlerShim.SetVirtualView at D:\a\_work\1\s\src\Compatibility\Core\src\RendererToHandlerShim.cs:149,5	C#
 	0x7 in Microsoft.Maui.Handlers.ViewHandler<Microsoft.Maui.IView,Android.Views.View>.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:56,4	C#
 	0x65 in Microsoft.Maui.Controls.Element.SetHandler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:64,6	C#
 	0x2 in Microsoft.Maui.Controls.Element.set_Handler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:20,11	C#
 	0x18 in Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\VisualElement\VisualElement.Impl.cs:301,5	C#
 	0xB0 in Microsoft.Maui.Platform.ElementExtensions.ToHandler at D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:96,4	C#
 	0x2 in Microsoft.Maui.Platform.ElementExtensions.ToPlatform at D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:127,4	C#
 	0xA6 in Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\Layout\LayoutHandler.Android.cs:43,5	C#
 	0x7 in Microsoft.Maui.Handlers.ViewHandler<Microsoft.Maui.ILayout,Microsoft.Maui.Platform.LayoutViewGroup>.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:56,4	C#
 	0x65 in Microsoft.Maui.Controls.Element.SetHandler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:64,6	C#
 	0x2 in Microsoft.Maui.Controls.Element.set_Handler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:20,11	C#
 	0x18 in Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\VisualElement\VisualElement.Impl.cs:301,5	C#
 	0xB0 in Microsoft.Maui.Platform.ElementExtensions.ToHandler at D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:96,4	C#
 	0x2 in Microsoft.Maui.Platform.ElementExtensions.ToPlatform at D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:127,4	C#
 	0x1D in Microsoft.Maui.Handlers.ScrollViewHandler.UpdateInsetView at D:\a\_work\1\s\src\Core\src\Handlers\ScrollView\ScrollViewHandler.Android.cs:175,4	C#
 	0x13 in Microsoft.Maui.Handlers.ScrollViewHandler.MapContent at D:\a\_work\1\s\src\Core\src\Handlers\ScrollView\ScrollViewHandler.Android.cs:109,4	C#
 	0x21 in Microsoft.Maui.PropertyMapper<Microsoft.Maui.IScrollView,Microsoft.Maui.Handlers.IScrollViewHandler>. at D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:183,6	C#
 	0x18 in Microsoft.Maui.PropertyMapper.UpdatePropertyCore at D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:47,4	C#
 	0x1D in Microsoft.Maui.PropertyMapper.UpdateProperties at D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:82,5	C#
 	0xCB in Microsoft.Maui.Handlers.ElementHandler.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:74,4	C#
 	0x2 in Microsoft.Maui.Handlers.ViewHandler.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandler.cs:125,4	C#
 	0x2 in Microsoft.Maui.Handlers.ViewHandler<Microsoft.Maui.IScrollView,Microsoft.Maui.Platform.MauiScrollView>.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:53,4	C#
 	0x7 in Microsoft.Maui.Handlers.ViewHandler<Microsoft.Maui.IScrollView,Microsoft.Maui.Platform.MauiScrollView>.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:56,4	C#
 	0x65 in Microsoft.Maui.Controls.Element.SetHandler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:64,6	C#
 	0x2 in Microsoft.Maui.Controls.Element.set_Handler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:20,11	C#
 	0x18 in Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\VisualElement\VisualElement.Impl.cs:301,5	C#
 	0xB0 in Microsoft.Maui.Platform.ElementExtensions.ToHandler at D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:96,4	C#
 	0x2 in Microsoft.Maui.Platform.ElementExtensions.ToPlatform at D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:127,4	C#
 	0x60 in Microsoft.Maui.Handlers.ContentViewHandler.UpdateContent at D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:44,5	C#
 	0x1 in Microsoft.Maui.Handlers.ContentViewHandler.MapContent at D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:49,4	C#
 	0x21 in Microsoft.Maui.PropertyMapper<Microsoft.Maui.IContentView,Microsoft.Maui.Handlers.IContentViewHandler>. at D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:183,6	C#
 	0x18 in Microsoft.Maui.PropertyMapper.UpdatePropertyCore at D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:47,4	C#
 	0x1D in Microsoft.Maui.PropertyMapper.UpdateProperties at D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:82,5	C#
 	0xCB in Microsoft.Maui.Handlers.ElementHandler.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:74,4	C#
 	0x2 in Microsoft.Maui.Handlers.ViewHandler.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandler.cs:125,4	C#
 	0x2 in Microsoft.Maui.Handlers.ViewHandler<Microsoft.Maui.IContentView,Microsoft.Maui.Platform.ContentViewGroup>.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:53,4	C#
 	0x2 in Microsoft.Maui.Handlers.ContentViewHandler.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:27,4	C#
 	0x7 in Microsoft.Maui.Handlers.ViewHandler<Microsoft.Maui.IContentView,Microsoft.Maui.Platform.ContentViewGroup>.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:56,4	C#
 	0x65 in Microsoft.Maui.Controls.Element.SetHandler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:64,6	C#
 	0x2 in Microsoft.Maui.Controls.Element.set_Handler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:20,11	C#
 	0x18 in Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\VisualElement\VisualElement.Impl.cs:301,5	C#
 	0xB0 in Microsoft.Maui.Platform.ElementExtensions.ToHandler at D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:96,4	C#
 	0x2 in Microsoft.Maui.Platform.ViewExtensions.ToHandler at D:\a\_work\1\s\src\Core\src\Platform\ViewExtensions.cs:44,4	C#
 	0x92 in Microsoft.Maui.Controls.Platform.Compatibility.ShellFragmentContainer.OnCreateView at D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellFragmentContainer.cs:50,4	C#
 	0x24 in AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ at C:\a\_work\1\s\generated\androidx.fragment.fragment\obj\Release\net6.0-android\generated\src\AndroidX.Fragment.App.Fragment.cs:1999,4	C#
 	0xD in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:352,5	C#

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
FreakyAlicommented, Oct 31, 2022

Seems a custom control derived from AbsoluteLayout will cause this InvalidCastException. Using View/IView or ContentPage does not result in a crash. Is AbsoluteLayout not supported for handlers at all (by design) or is it a bug?

I have a similar issue as well man, the problem is my control is literally based on AbsoluteLayout and I have no clue what the heck am I supposed to do…

Not to mention the docs don’t specify ANYWHERE how to convert a VisualElementRenderer into a Maui Handler

1reaction
maexspcommented, Sep 14, 2022

Seems a custom control derived from AbsoluteLayout will cause this InvalidCastException. Using View/IView or ContentPage does not result in a crash. Is AbsoluteLayout not supported for handlers at all (by design) or is it a bug?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Specified cast is not valid (xamarin forms)
InvalidCastException is a runtime exception and the stack trace should show exactly which lines causes it. If you can't get a stack trace,...
Read more >
Specified cast is not valid. Xamarin.Forms
System.InvalidCastException: 'Specified cast is not valid.' Accroding to what i've found the issue is with the ListView block, but im not ...
Read more >
How do I resolve specified cast is not valid (xamarin. ...
The error message is pretty explicit: Specified cast is not valid. Means exactly what it says: you have an object that you are...
Read more >
Xamarin Community Forums
Strange System.InvalidCastException: Specified cast is not valid ... Hi, I'm running into this Specified cast is not valid issue when I do a...
Read more >
System.InvalidCastException: Specified cast is not valid. Error
InvalidCastException : Specified cast is not valid. Error Note: The information provided in this video is as it is with no modifications.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found