xamarin 是否从未调用MAUI CreatePlatformView?

8wtpewkr  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(139)

更新日期:

所以我不确定这是否是一个bug,但我已经在Github中提出了一个bug,可以在这里跟踪:https://github.com/dotnet/maui/issues/9720

问题:

所以我最近一直在广泛地尝试MAUI,并试图创建一个自定义控件,我猜,我遇到了这个奇怪的问题,CreatePlatform方法从未被调用,起初我认为这是因为我使用的是MAUI类库,它们有一些问题,因此,我在同一个MAUI项目中创建了另一个控件,而不是通过CL来创建,令我惊讶的是,即使这样它也不起作用。
我的代码如下:
接口:

public interface IExtendedLabel : ILabel
{
    bool HasUnderline { get; }
    Color UnderlineColor { get; }
}

标注分类:

public class ExtendedLabel : Label, IExtendedLabel
{
    public readonly BindableProperty HasUnderlineProperty = BindableProperty.Create(
        nameof(HasUnderline),
        typeof(bool),
        typeof(ExtendedLabel),
        true);

    public bool HasUnderline
    {
        get => (bool)GetValue(HasUnderlineProperty);
        set => SetValue(HasUnderlineProperty, value);
    }

    public readonly BindableProperty UnderlineColorProperty = BindableProperty.Create(
       nameof(UnderlineColor),
       typeof(Color),
       typeof(ExtendedLabel),
       Colors.Black);

    public Color UnderlineColor
    {
        get => (Color)GetValue(HasUnderlineProperty);
        set => SetValue(HasUnderlineProperty, value);
    }
}

我的共享处理程序:

using System;
using MAUI.FreakyControls;
using Microsoft.Maui.Handlers;
#if ANDROID
using NativeView = AndroidX.AppCompat.Widget.AppCompatTextView;
#endif
#if IOS
using NativeView = UIKit.UILabel;
#endif
namespace Samples
{
    public partial class ExtendedLabelHandler : ViewHandler<IExtendedLabel,NativeView>
    {
        #region ctor 

        public static CommandMapper<IExtendedLabel, ExtendedLabelHandler> CommandMapper = new(ViewCommandMapper);

        public ExtendedLabelHandler() : base(FreakyEditorMapper)
        {

        }

        public ExtendedLabelHandler(IPropertyMapper mapper = null) : base(mapper ?? FreakyEditorMapper)
        {

        }

        #endregion

        #region Mappers

        public static IPropertyMapper<IExtendedLabel, ExtendedLabelHandler> FreakyEditorMapper = new PropertyMapper<IExtendedLabel, ExtendedLabelHandler>(ViewMapper)
        {
            [nameof(IExtendedLabel.HasUnderline)] = MapHasUnderlineWithColor,
            [nameof(IExtendedLabel.UnderlineColor)] = MapHasUnderlineWithColor
        };

        public static void MapHasUnderlineWithColor(ExtendedLabelHandler handler, IExtendedLabel entry)
        {

        }

        #endregion
    }
}

Android处理程序:

public partial class ExtendedLabelHandler
    {
        protected override AppCompatTextView CreatePlatformView()
        {
            var nativeView = new AppCompatTextView(this.Context)
            {

            };
            return nativeView;
        }

        private void HandleNativeHasUnderline(bool hasUnderline, Color underlineColor)
        {
            if (hasUnderline)
            {
                var AndroidColor = underlineColor.ToNativeColor();
                var colorFilter = BlendModeColorFilterCompat.CreateBlendModeColorFilterCompat(
                    AndroidColor, BlendModeCompat.SrcIn);
                PlatformView.Background?.SetColorFilter(colorFilter);
            }
            else
            {
                PlatformView.Background?.ClearColorFilter();
            }
        }
    }

我的iOS处理程序:

public partial class ExtendedLabelHandler
    {
        CoreAnimation.CALayer bottomLine;

        protected override UILabel CreatePlatformView()
        {
            return new UILabel();
        }

        private void HandleNativeHasUnderline(bool hasUnderline, Color underlineColor)
        {
            if (hasUnderline)
            {
                var uiColor = underlineColor.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();
            }
        }
    }

添加处理程序:

handlers.AddHandler(typeof(IExtendedLabel), typeof(ExtendedLabelHandler));

我做错什么了吗?
你可以在我的repo上找到完整的代码,这里有一个完整的工作示例,说明由于某种原因,该方法从未被调用过:https://github.com/FreakyAli/MAUI.FreakyControls/tree/r1-gh/feat/freakyeditor

vh0rcniy

vh0rcniy1#

所以问题是我的注册,我注册的是我的接口,而不是我的自定义控件的类:

handlers.AddHandler(typeof(ExtendedLabel), typeof(ExtendedLabelHandler));

祝所有找这个的人好运。

相关问题