【问题标题】:MAUI CreatePlatformView is never called?从未调用 MAUI CreatePlatformView?
【发布时间】:2022-12-15 10:12:58
【问题描述】:

更新 :

所以我不确定这是否是一个错误,但无论如何我已经在 Github 上提出了一个,可以在这里跟踪: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
    }
}

处理程序安卓:

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));

难道我做错了什么?

您可以在我的仓库中找到完整的代码,其中有一个完整的工作示例,该方法由于某种原因从未被调用:https://github.com/FreakyAli/MAUI.FreakyControls/tree/r1-gh/feat/freakyeditor

【问题讨论】:

  • 貌似和官方文档中创建自定义控件的步骤有些不一样,比如控件接口要继承自IView。你可以在this link和github上的example查看答案。
  • 但这没有意义,如果控制界面只有在我继承 IView 时才能工作,那只意味着如果我想利用现有的控件并在它们之上构建,那将无法工作......
  • 您是否尝试按照我提供的链接中的步骤创建自定义控件?
  • @LiyunZhang-MSFT 显然这个问题完全不同

标签: c# xamarin xamarin.forms maui .net-maui


【解决方案1】:

所以问题是我的注册,我正在注册我的界面而不是我的自定义控件的类:

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

祝所有寻找这个的人好运。

【讨论】:

    【解决方案2】:

    如何覆盖自定义处理程序中的方法,如 handler.PlatformView.CanPerform()?

    如何覆盖方法以禁用条目自定义处理程序中的粘贴选项/菜单,因为它会导致粘贴问题。 https://github.com/dotnet/maui/issues/10302

    任何帮助表示赞赏!

    【讨论】:

    猜你喜欢
    • 2023-01-18
    • 2022-11-03
    • 1970-01-01
    • 2022-01-07
    • 2022-10-07
    • 2022-10-01
    • 2022-06-15
    • 2022-10-25
    • 2022-12-10
    相关资源
    最近更新 更多