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

What handler / mapper to call `AppendMapping` on?

Open hansmbakker opened this issue 1 year ago • 1 comments

Type of issue

Missing information

Description

In https://github.com/gzadro/.NET-MAUI-custom-Entry-control, the mapping code is run by calling Microsoft.Maui.Handlers.ElementHandler.ElementMapper.AppendToMapping(IView, Action): https://github.com/gzadro/.NET-MAUI-custom-Entry-control/blob/f3dea0697762e6c8492a8259f71dc77ee3c78285/CustomInputMaui/MauiProgram.cs#L24-L30

In the Map method, the handler is being cast to an IEntryHandler after all: https://github.com/gzadro/.NET-MAUI-custom-Entry-control/blob/f3dea0697762e6c8492a8259f71dc77ee3c78285/CustomInputMaui/Platforms/Android/CustomEntryMapper.cs#L21-L22

Looking at the API, I would find it more intuitive to use Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping(IEntry, Action) (like in the documentation about handlers) but it seems to stop functioning when I use that method. If that downside would not be there, then it would be possible to avoid the casting.

I can't find documentation on where the difference is explained, though.

Can you please explain

  • what the difference between EntryHandler and ElementHandler is?
  • what the difference between EntryHandler.Mapper and ElementHandler.ElementMapper is?

Page URL

https://learn.microsoft.com/en-us/dotnet/maui/user-interface/handlers/customize?view=net-maui-8.0

Content source URL

https://github.com/dotnet/docs-maui/blob/main/docs/user-interface/handlers/customize.md

Document Version Independent Id

70227b04-808f-1370-186b-10cfef525959

Article author

@davidbritch

Metadata

  • ID: 659665da-2ff3-3550-2e0c-20902c15c3e6
  • Service: dotnet-mobile
  • Sub-service: dotnet-maui

hansmbakker avatar Jan 29 '24 17:01 hansmbakker

Update - for me, empirically it seems that:

  • ElementHandler.ElementMapper should be used for custom-defined properties in an inherited class (e.g. if you add CornerRadius yourself to a subclass of Entry). A custom-defined CornerRadius mapping is not called via EntryHandler.Mapper for me (CornerRadius is not implemented on the out-of-the-box Entry class).
  • EntryHandler.Mapper should be used for built-in properties from the "built-in properties" like BackgroundColor or IsEnabled. The mappings for these properties do not work for me if I configure them using ElementHandler.ElementMapper.

It would really help me to have this explicitly documented though, as I'm not sure if this is the intended behavior, and if so - how should people know what property to map where?

hansmbakker avatar Jan 30 '24 12:01 hansmbakker