[英]MAUI CreatePlatformView is never called?
UPDATE:更新:
So i am not sure if this is a bug or not but I have raised one in Github anyway, which can be tracked here: https://github.com/dotnet/maui/issues/9720所以我不确定这是否是一个错误,但无论如何我已经在 Github 中提出了一个错误,可以在此处跟踪: https://github.com/dotnet/maui/issues/9720
QUESTION:问题:
So I have been trying out MAUI extensively lately and was trying to Create a Custom Control I guess and I ran into this weird problem, the CreatePlatform method was never getting called, at first I thought this was because I was using a MAUI class library and there was some issue with them, So instead I created another control in the same MAUI project instead of doing it through a CL and to my surprise even then it did not work.所以我最近一直在广泛尝试 MAUI,并试图创建一个自定义控件,我猜我遇到了这个奇怪的问题,CreatePlatform 方法从未被调用,起初我认为这是因为我使用的是 MAUI class 库和他们有一些问题,所以我在同一个 MAUI 项目中创建了另一个控件,而不是通过 CL 来完成它,令我惊讶的是,即使这样它也不起作用。
My code is as follows:我的代码如下:
Interface:界面:
public interface IExtendedLabel : ILabel
{
bool HasUnderline { get; }
Color UnderlineColor { get; }
}
Label class: Label class:
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);
}
}
My shared handler:我的共享处理程序:
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
}
}
Handler Android:处理程序 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();
}
}
}
My iOS handler:我的 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();
}
}
}
Adding the handler:添加处理程序:
handlers.AddHandler(typeof(IExtendedLabel), typeof(ExtendedLabelHandler));
Am I doing something wrong?难道我做错了什么?
You can find the full code on my repo here which has a full working example of the method never getting called for some reason: https://github.com/FreakyAli/MAUI.FreakyControls/tree/r1-gh/feat/freakyeditor您可以在我的 repo 上找到完整的代码,其中包含该方法的完整工作示例,由于某种原因永远不会被调用: https://github.com/FreakyAli/MAUI.FreakyControls/tree/r1-gh/feat/freakyeditor
So the issue was my registration, I was registering my interface instead of the class of my custom control:所以问题是我的注册,我正在注册我的界面而不是我的自定义控件的 class:
handlers.AddHandler(typeof(ExtendedLabel), typeof(ExtendedLabelHandler));
Goodluck to anyone looking for this.祝任何寻找这个的人好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.