So initially while I was creating my Custom Control I was facing an issue where the handlers were not calling the CreatePlatform method, I was able to solve that with some help from you guys more information: MAUI CreatePlatformView is never called?
But ever since I have done that it seems that a lot of properties of my Editor have stopped working which is really weird since they should by default get wired from the existing MAUI control.
Here is the relevant code:
Shared code handler:
using System;
using Microsoft.Maui.Handlers;
#if ANDROID
using NativeView = AndroidX.AppCompat.Widget.AppCompatEditText;
#endif
#if IOS
using NativeView = UIKit.UITextView;
#endif
public partial class FreakyEditorHandler : ViewHandler<IFreakyEditor, NativeView>
{
#region ctor
public static CommandMapper<IFreakyEditor, FreakyEditorHandler> CommandMapper = new(ViewCommandMapper);
public FreakyEditorHandler() : base(FreakyEditorMapper)
{
}
public FreakyEditorHandler(IPropertyMapper pmapper, CommandMapper cmapper = null)
: base(pmapper ?? FreakyEditorMapper, cmapper ?? CommandMapper)
{
}
#endregion
#region Mappers
public static IPropertyMapper<IFreakyEditor, FreakyEditorHandler> FreakyEditorMapper = new PropertyMapper<IFreakyEditor, FreakyEditorHandler>(ViewMapper)
{
[nameof(IFreakyEditor.HasUnderline)] = MapHasUnderlineWithColor,
[nameof(IFreakyEditor.UnderlineColor)] = MapHasUnderlineWithColor
};
public static void MapHasUnderlineWithColor(FreakyEditorHandler handler, IFreakyEditor entry)
{
handler.HandleNativeHasUnderline(entry.HasUnderline, entry.UnderlineColor);
}
#endregion
protected override void ConnectHandler(NativeView platformView)
{
base.ConnectHandler(platformView);
}
protected override void DisconnectHandler(NativeView platformView)
{
base.DisconnectHandler(platformView);
}
}
My Native Handlers are as follows:
Android:
using System;
using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Views;
using Android.Views.InputMethods;
using Android.Widget;
using AndroidX.AppCompat.Widget;
using AndroidX.Core.Content;
using AndroidX.Core.Content.Resources;
using AndroidX.Core.Graphics;
using AndroidX.Core.View;
using Microsoft.Maui.Handlers;
using Color = Microsoft.Maui.Graphics.Color;
namespace MAUI.FreakyControls
{
public partial class FreakyEditorHandler
{
protected override AppCompatEditText CreatePlatformView()
{
var _nativeView = new AppCompatEditText(Context)
{
ImeOptions = ImeAction.Done,
Gravity = GravityFlags.Top,
TextAlignment = Android.Views.TextAlignment.ViewStart,
};
_nativeView.SetSingleLine(false);
_nativeView.SetHorizontallyScrolling(false);
return _nativeView;
}
private void HandleNativeHasUnderline(bool hasUnderline, Color underlineColor)
{
ColorStateList colorStateList;
var AndroidColor = underlineColor.ToNativeColor();
colorStateList = ColorStateList.ValueOf(hasUnderline ? AndroidColor : Android.Graphics.Color.Transparent);
ViewCompat.SetBackgroundTintList(PlatformView, colorStateList);
//colorFilter = hasUnderline ? ContextCompat.GetColor(this, AndroidColor, PorterDuff.Mode.SrcAtop);
//BlendModeColorFilterCompat.CreateBlendModeColorFilterCompat(
// AndroidColor, BlendModeCompat.SrcAtop) :
//BlendModeColorFilterCompat.CreateBlendModeColorFilterCompat(
// Android.Graphics.Color.Transparent, BlendModeCompat.SrcAtop);
//PlatformView.Background?.SetColorFilter(colorFilter);
}
}
}
iOS :
using System;
using CoreGraphics;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using UIKit;
using CoreAnimation;
using static MAUI.FreakyControls.Platforms.iOS.NativeExtensions;
namespace MAUI.FreakyControls
{
public partial class FreakyEditorHandler
{
CALayer bottomline;
protected override UITextView CreatePlatformView()
{
var _nativeView = new UITextView();
return _nativeView;
}
private void HandleNativeHasUnderline(bool hasUnderline, Color color)
{
if (hasUnderline)
{
var uiColor = color.ToNativeColor();
bottomline = BottomLineDrawer(uiColor);
bottomline.Frame = new CGRect(x: 0, y: PlatformView.Frame.Size.Height - 5,
width: PlatformView.Frame.Size.Width, height: 1);
PlatformView.Layer.AddSublayer(bottomline);
PlatformView.Layer.MasksToBounds = true;
}
else
{
bottomline?.RemoveFromSuperLayer();
}
}
}
}
Handler registration :
handlers.AddHandler(typeof(FreakyEditor), typeof(FreakyEditorHandler));
You can access my code base here: https://github.com/FreakyAli/MAUI.FreakyControls/tree/r1-gh/feat/freakyeditor