MAUI CreatePlatformView 从未被调用?

2024-02-22

我最近一直在广泛尝试 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 https://github.com/FreakyAli/MAUI.FreakyControls/tree/r1-gh/feat/freakyeditor

Update

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


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

 handlers.AddHandler(typeof(ExtendedLabel), typeof(ExtendedLabelHandler));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MAUI CreatePlatformView 从未被调用? 的相关文章

随机推荐

  • Ruby 中私有方法应该放在哪里?

    大多数博客或教程或书籍在任何类 模块的底部都有私有方法 这是最佳实践吗 我发现在必要时使用私有方法更方便 例如 public def my method do something minion method end private def
  • Python 正则表达式中的错误? (re.sub 与 re.MULTILINE)

    我注意到 Python 的 Regex 库中有一些奇怪的行为 并且我不确定我是否做错了什么 如果我使用正则表达式对其运行re sub with re MULTILINE 它似乎只替换了前几次出现的情况 如果我关闭它 它会替换所有出现的情况r
  • 有没有办法在python nltk中反转stem?

    我有一个 NLTK python 中的词干列表 并且想要获取创建该词干的可能单词 有没有办法在Python中获取词干并获取词干的单词列表 据我所知 答案是否定的 并且根据词干分析器的不同 可能很难进行详尽的搜索来恢复词干规则的效果 并且无论
  • Zend_Tool:致命错误:无法重新声明类 Zend_Loader

    我能够创建新项目 zf create project ProjectName 但这会创建一个项目而不复制 Zend Framework 库 所以我将 ZF 复制到我的项目库中并尝试创建一个控制器 zf create controller a
  • 当切片索引超出范围时如何引发 IndexError?

    The Python 文档 https docs python org 2 library exceptions html exceptions IndexError指出 切片索引被无声地截断掉在允许范围 因此没有IndexErrors切片
  • 获取 PDF 中确切的字符串位置

    我尝试读取流并希望获得每个字符串的确切位置 坐标 int size reader getXrefSize for int i 0 i lt size i PdfObject pdfObject reader getPdfObject i i
  • 为什么退出 Ruby 线程会终止我的整个程序?

    我有这段代码 puts Start loop do Thread start do puts Hello from thread exit end text gets puts text end puts Done 我期望看到 开始 然后是
  • 有没有办法运行两个调试器实例?

    我正在从事一个多人游戏项目 它在 Cirrus 上运行 最好能够调试正在运行的游戏的两个实例 而不仅仅是一个 但是一旦我发送新的调试调用 Flash Builder 就会停止一个调试实例 如果有办法让 2 个调试实例同时运行 您有什么想法吗
  • 有没有办法在 Travis CI 构建中启动 android 模拟器?

    I have adb 的 python 包装库 https github com vmalyi adb lib我有依赖于模拟器或真实设备的单元测试 因为它们执行 adb 命令 我还想使用 Travis CI 作为构建环境 并为每个构建执行这
  • Underscore.js 过滤掉唯一值

    这里有一个link https jsfiddle net 6xytngz7 到我的小提琴 我有一个根据 Ajax 响应获取的 JSON 动态创建的表 我的表中有重复的条目 例如xxx xxx 我想将它们分组并像这样 td xxx td 我尝
  • sun.reflect.generics.ReflectiveObjects.NotImplementedException 和 org.apache.commons.lang3.NotImplementedException 之间有什么区别

    我发现有2个NotImplementedException sun reflect generics reflectiveObjects NotImplementedException org apache commons lang3 No
  • Vue-router组件复用

    我想知道如何停止 Vue router 中的组件重用 我正在构建一个简单的页面应用程序 但我无法单击同一链接两次后更新数据 是否有可能以某种方式强制重新加载或者我的情况的最佳实践是什么 Use the key属性上router view设置
  • 在 Swagger UI/Swashbuckle 中实施 OpenID Connect 的解决方法

    我有两个不同的项目 Auth 项目 实现 IdentityServer4 以提供身份验证服务 API项目 实现Swashbuckle AspNetCore 5 5来生成Swagger UI 我正在使用SecuritySchemeType O
  • 哪个 cygwin 镜像站点是完整的? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我注意到 cygwin 镜像站点中存在严重不一致的情况 默认安装的软件包取决于您选择的镜像 更糟糕的是 某些网站缺少重要的软件包 因此您无
  • SQL Server 选择结果作为用“,”分隔的字符串[重复]

    这个问题在这里已经有答案了 我有一个select返回一列的查询 我想将其转换为用 分隔的字符串行 Select name from tblUsers 给出结果 Row1 asieh Row2 amir Row3 safoora 我想回来 R
  • 使用 Web Matrix 助手进行 C# JSON 解码

    我一直在寻找一种将 JSON 对象转换为动态对象的简洁方法 我可以转换为一个对象 但 Twitter Streaming API 实际上发送两个不同的 JSON 对象 并且可能是未来的对象类型 我当前使用的代码来自 将 JSON 反序列化为
  • 如何忽略二进制序列化的 Event 类成员?

    我需要避免序列化 Event 类成员 因为当事件由未标记为可序列化的对象处理时 序列化将失败 我尝试在 Event 类成员上使用 NonSerialized 属性 但无法编译 这行代码
  • Flutter-GestureDetector 不适用于堆栈中的容器

    我在一个堆栈中有两个容器 两个容器都有 GestureDetector 第一个容器的 OnTap 工作正常 但它不能与另一个容器一起工作 第一个容器是图像 第二个容器是部分与第一个容器对齐的绿色背景 new Stack alignment
  • Java - 替代许多带有instanceof条件的else if语句

    我有一段混乱的代码 我真的很想清理它 因为它有大约 12 个 else if 并且每个 if 语句都会检查 2 个对象的实例 所以类似于 If parentObj 超类实例 ParentObj2 超类实例 每个 if 语句执行不同的东西 所
  • MAUI CreatePlatformView 从未被调用?

    我最近一直在广泛尝试 MAUI 并试图创建一个自定义控件 我猜 我遇到了这个奇怪的问题 CreatePlatform 方法从未被调用 起初我认为这是因为我正在使用 MAUI 类库 并且有他们有一些问题 所以我在同一个 MAUI 项目中创建了