dotnet / dotnet/docs-maui

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

Open
#1,572 0 comments 0 reactions 0 assignees View on GitHub
doc-enhancement dotnet-maui/tech dotnet-mobile/prod Pri2 upgrade-from-xamarin
Dominant language
No language data
Stars
282
Forks
265
Avg merge
2d 2h
Merged PRs (30d)
19

Description

The described code in "[Reuse custom renderers in .NET MAUI](https://learn.microsoft.com/en-us/dotnet/maui/migration/custom-renderers)" is only valid for the[ referenced example](https://learn.microsoft.com/en-us/samples/dotnet/maui-samples/custom-renderers/), but not for any custom renderer.

I tried to migrate an `Entry `CustomRenderer and got an InvalidCastException
![image](https://github.com/dotnet/docs-maui/assets/6480047/acea2a86-59aa-4dcf-90c4-16f47a968605)

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](https://github.com/ericbrunner/TaskyApp.Maui.SingleProject/commit/7fb03696f511b90a725ad9c943c1c8d248ab7726).

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.*

* ID: c787fde2-1c9c-62d4-2cdd-93681e0c1ff5
* Version Independent ID: c787fde2-1c9c-62d4-2cdd-93681e0c1ff5
* Content: [Reuse custom renderers in .NET MAUI - .NET MAUI](https://learn.microsoft.com/en-us/dotnet/maui/migration/custom-renderers)
* Content Source: [docs/migration/custom-renderers.md](https://github.com/dotnet/docs-maui/blob/main/docs/migration/custom-renderers.md)
* Product: **dotnet-mobile**
* Technology: **dotnet-maui**
* GitHub Login: @davidbritch
* Microsoft Alias: **dabritch**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.