dotnet / dotnet/docs-maui

Unregister events added in `Mapper.AppendToMapping`

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

Description

There is the tutorial for customization of controls here https://learn.microsoft.com/en-us/dotnet/maui/user-interface/handlers/customize?view=net-maui-7.0#customize-a-control. The tutorial contains the following sample:

```cs
namespace CustomizeHandlersDemo.Views;

public partial class CustomizeEntryPage : ContentPage
{
public CustomizeEntryPage()
{
InitializeComponent();
ModifyEntry();
}

void ModifyEntry()
{
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
{
#if ANDROID
handler.PlatformView.SetSelectAllOnFocus(true);
#elif IOS || MACCATALYST
handler.PlatformView.EditingDidBegin += (s, e) => // WHERE TO UNREGISTER THIS?
{
handler.PlatformView.PerformSelector(new ObjCRuntime.Selector("selectAll"), null, 0.0f);
};
#elif WINDOWS
handler.PlatformView.GotFocus += (s, e) => // WHERE TO UNREGISTER THIS?
{
handler.PlatformView.SelectAll();
};
#endif
});
}
}
```

I wonder where to unregister registered events. I mean where can I put `handler.PlatformView.GotFocus -= ...` (not the minus sign)?

I'm aware that unregistering of events is optional but then it is a good practice to unregister events anyway. Could you please provide some guidance on this?

Thank you

---
#### Document Details

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

* ID: 659665da-2ff3-3550-2e0c-20902c15c3e6
* Version Independent ID: 70227b04-808f-1370-186b-10cfef525959
* Content: [.NET MAUI control customization with handlers - .NET MAUI](https://learn.microsoft.com/en-us/dotnet/maui/user-interface/handlers/customize?view=net-maui-7.0#customize-a-control)
* Content Source: [docs/user-interface/handlers/customize.md](https://github.com/dotnet/docs-maui/blob/main/docs/user-interface/handlers/customize.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.