docs-maui icon indicating copy to clipboard operation
docs-maui copied to clipboard

Documentation not sufficient for any Xamarin.Forms CustomRenderer Migration in .NET MAUI

Open ericbrunner opened this issue 2 years ago • 0 comments

The described code in "Reuse custom renderers in .NET MAUI" is only valid for the referenced example, but not for any custom renderer.

I tried to migrate an Entry CustomRenderer and got an InvalidCastException image

with the code provided from the docs in the MauiProgram.cs

Workaround: When I add.UseMauiCompatibility() to the MauiAppBuilder and call the .AddCompatibilityRenderer like:

 var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiCompatibility()
            .UseMauiCommunityToolkit()
            .UseMauiApp(serviceProvider => new App(serviceProvider))
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            })
            .ConfigureMauiHandlers((mauiHandlerCollection) =>
            {
#if ANDROID
                mauiHandlerCollection.AddCompatibilityRenderer(
                    typeof(MyEntry),
                    typeof(TaskyApp.Droid.CustomRenderer.MyEntryRenderer));
#elif IOS
                mauiHandlerCollection.AddCompatibilityRenderer(
                    typeof(MyEntry),
                    typeof(TaskyApp.iOS.CustomRenderer.MyEntryRenderer));
#endif
            });

it works.

Code example is provided here.

It would be good when the docs get updated to capture any custom renderer reuse.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

ericbrunner avatar Jun 30 '23 04:06 ericbrunner