Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

671
Visualizações
.NET Maui Custom Handlers not working, official docs is wrong?

I am making an app using .NET MAUI and I am trying to implement custom handlers for specific instances of controls (ex. some entries should use a custom handler I created). To achieve this I followed the official MS docs for this. The following is the setup they tell me to use:

1.First make a subclass of the Entry control:

using Microsoft.Maui.Controls;

namespace MyMauiApp
{
    public class MyEntry : Entry
    {
    }
}

2.I then customize the EntryHandler to perform the desired modification to MyEntry instances:

using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;

namespace MauiApp1
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            Microsoft.Maui.Handlers.EntryHandler.EntryMapper[nameof(IView.Background)] = (handler, view) =>
            {
                if (view is MyEntry)
                {
#if __ANDROID__
                    handler.NativeView.SetBackgroundColor(Colors.Red.ToNative());
#elif __IOS__
                  handler.NativeView.BackgroundColor = Colors.Red.ToNative();
                  handler.NativeView.BorderStyle = UIKit.UITextBorderStyle.Line;
#elif WINDOWS
                  handler.NativeView.Background = Colors.Red.ToNative();
#endif
                }
            };
        }
    }
}

PROBLEM: This gives me the following error:

Severity Code Description Project File Line Suppression State Error CS0021 Cannot apply indexing with [] to an expression of type 'IPropertyMapper<IEntry, EntryHandler>' MyMauiApp (net6.0-android), MyMauiApp (net6.0-ios), MyMauiApp (net6.0-windows10.0.19041) C:\Users\xxxxxx\source\repos\MyMauiApp\MyMauiApp\App.xaml.cs 24 Active

As I said I followed the docs completely but still this error. I have read that other people have this issue too. Can anyone help?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It seems some breaking changes have been made in this area by the means of this pr here and here.

From what it looks like this has been done so that you can cascade customizations in mappers with AppendToMapping and PrependToMapping or modify the whole mapping altogether with ModifyMapping.

Without explaining all the variations here, let's focus on your situation. This means that instead of this line Microsoft.Maui.Handlers.EntryHandler.EntryMapper[nameof(IView.Background)] = (handler, view) =>

You should now declare this as: Microsoft.Maui.Handlers.EntryHandler.EntryMapper.AppendToMapping(nameof(IView.Background), (handler, view) =>

Note that you should now add a ) on the closing bracket too, making the full code:

Microsoft.Maui.Handlers.EntryHandler.EntryMapper.AppendToMapping(nameof(IView.Background), (handler, view) =>
{
    if (view is MyEntry)
    {
#if __ANDROID__
        handler.NativeView.SetBackgroundColor(Colors.Red.ToNative());
#elif __IOS__
        handler.NativeView.BackgroundColor = Colors.Red.ToNative();
        handler.NativeView.BorderStyle = UIKit.UITextBorderStyle.Line;
#elif WINDOWS
        handler.NativeView.Background = Colors.Red.ToNative();
#endif
    }
});

I will see if I can update the Docs here and there and hopefully this won't break again ;)

Edit: Updated the wiki page for this

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda