自定义渲染器在 iOS + 库中不起作用

Mat*_*zzo 5 forms custom-renderer ios xamarin

我不知道为什么我的 Xamarin.Forms 自定义渲染器在我将它放入库中并且仅在 iOS 上不起作用时,有人可以帮助我吗?

[assembly: ExportRenderer(typeof(HtmlLabel), typeof(HtmlLabelRenderer))]
namespace Plugin.HtmlLabel.iOS
{
    public class HtmlLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            if (Control == null) return;
            UpdateMaxLines();
            UpdateText();
        }
Run Code Online (Sandbox Code Playgroud)

如果进入项目,它在 Android、UWP 和 iOS 上运行良好。

https://github.com/matteobortolazzo/HtmlLabelPlugin

Sus*_*ver 5

向您的类添加一个不执行任何操作的Initialize静态方法,以确保您的渲染器类型在Forms之前HtmlLabelRenderer加载

例子:

public static void Initialize()
{
}
Run Code Online (Sandbox Code Playgroud)

用法:

在您的AppDelegate before Forms.Init()中,调用您的 Initialize 方法:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    Plugin.HtmlLabel.iOS.HtmlLabelRenderer.Initialize();
    global::Xamarin.Forms.Forms.Init();

    LoadApplication(new App());

    return base.FinishedLaunching(app, options);
}
Run Code Online (Sandbox Code Playgroud)