Using XF renderer in .NET MAUI not possible -> System.InvalidCastException: 'Specified cast is not valid.'
- Dominant language
- C#
- Stars
- 23.3k
- Forks
- 2k
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 296
Description
### 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:
```cs
public class CardsView : AbsoluteLayout
{
public CardsView()
{
Background = Colors.Red;
}
}
```
Renderer:
```cs
public class CardsViewRenderer : VisualElementRenderer
{
public CardsViewRenderer(Context context) : base(context)
{
}
}
```
MauiProgram:
```cs
.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.GetHandler at D:\a\_work\1\s\src\Controls\src\Core\Registrar.cs:106,6 C#
0x42 in Microsoft.Maui.Controls.Internals.Registrar.GetHandlerForObject 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.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.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. 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.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:53,4 C#
0x7 in Microsoft.Maui.Handlers.ViewHandler.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. 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.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.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#
```
Contributor guide
Research direction
Start by running the RendererDemo reproduction on an Android 10 emulator and inspect the failure at src/Controls/src/Core/Registrar.cs and src/Compatibility/Core/src/RendererToHandlerShim.Android.cs. Trace how AddCompatibilityRenderer resolves CardsViewRenderer and identify what completion means: the existing Xamarin.Forms renderer should load without the InvalidCastException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- mobile-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 38/100