dotnet / dotnet/maui

[Request] Allow self-registration of handlers, etc.

Open
#7,258 6 comments 20 reactions 0 assignees View on GitHub
area-controls-general partner proposal/open t/enhancement ☀️
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

### Description

Imagine a library with a big amount of handlers, effects, etc.

- If a client-dev wants to use the components - he needs to put a lot of code in the CreateMauiApp() method. If the client-dev uses a NuGet - it will not be obvious that there is an additional step he needs to do to get the components working.
- We can implement a single `UseLibrary()` extension method, but there are huge consequences for using such an approach. The problem is as follows - in the UseLibrary() method, we need to register everything - all handlers, renderers, effects, everything. Now the linker will not be able to strip away unused code - even if the client-dev uses only a couple of our controls.
- It will be very useful if we can introduce a property in the MauiApp class that will allow to dynamically register a handler. For example, the static constructor of a Component1 can execute a call like MauiApp.Current.TryAddHandler(Component1, Component1Handler). This way if a client-dev uses Component1 - the handler will be registered automatically. If the client-dev does not use it - the linker would be able to properly strip away the component and the handler. Note that the name of the method is TryAddHandler() - so it can check if a handler was already registered for this type and not overwrite it.

### Public API Changes

We have to analyze possibilities to allow this.

### Intended Use-Case

`MauiApp.Current.TryAddHandler(Component1, Component1Handler)`

Contributor guide

Open the contributing guide

Research direction

Start by examining CreateMauiApp(), the existing UseLibrary() registration approach, and the MauiApp API around the proposed TryAddHandler(Component1, Component1Handler) call. Determine whether self-registration can preserve linker trimming and avoid overwriting existing handlers; the work is done when a viable public API and its trade-offs are documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
mobile-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.