[英]MAUI CreatePlatformView is never called?
更新:
所以我不確定這是否是一個錯誤,但無論如何我已經在 Github 中提出了一個錯誤,可以在此處跟蹤: https://github.com/dotnet/maui/issues/9720
問題:
所以我最近一直在廣泛嘗試 MAUI,並試圖創建一個自定義控件,我猜我遇到了這個奇怪的問題,CreatePlatform 方法從未被調用,起初我認為這是因為我使用的是 MAUI class 庫和他們有一些問題,所以我在同一個 MAUI 項目中創建了另一個控件,而不是通過 CL 來完成它,令我驚訝的是,即使這樣它也不起作用。
我的代碼如下:
界面:
public interface IExtendedLabel : ILabel
{
bool HasUnderline { get; }
Color UnderlineColor { get; }
}
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);
}
}
我的共享處理程序:
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
所以問題是我的注冊,我正在注冊我的界面而不是我的自定義控件的 class:
handlers.AddHandler(typeof(ExtendedLabel), typeof(ExtendedLabelHandler));
祝任何尋找這個的人好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.