Why maui control do not need Addhandler but create custom control need?

mc 3,701 Reputation points
2023-04-24T04:07:11.3333333+00:00

as we know there are many controls such as StackLayout Grid in maui. but all of these do not need AddHandler to add the control but If I create custom control I have to add it . why?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,969 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 36,951 Reputation points Microsoft Vendor
    2023-04-24T07:38:52.6866667+00:00

    Hello,

    MAUI's internal controls do not need AddHandler to register because the handlers of these internal controls have been designed by the developer, and MAUI knows what handlers these controls correspond to after initialization, so no additional registration is required.

    For your custom control, MAUI needs to use the AddHandler method to understand what the handler corresponds to.

    Please refer to Create a custom control using handlers - Register the handler for more details.

    A custom control and its handler must be registered with an app, before it can be consumed. This should occur in the CreateMauiApp method in the MauiProgram class in your app project, which is the cross-platform entry point for the app.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.