Home > Net >  .NET Maui Custom Handlers not working, official docs is wrong?
.NET Maui Custom Handlers not working, official docs is wrong?

Time:11-26

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?

CodePudding user response:

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

  • Related