Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom renderer doesn't work in iOS + library

I don't know why my Xamarin.Forms custom renderer doesn't work if I put it into a library and only on iOS, somebody could help me?

[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();
        }

It works fine on Android, UWP and iOS if into the project.

https://github.com/matteobortolazzo/HtmlLabelPlugin

like image 578
Matteo Bortolazzo Avatar asked May 29 '17 15:05

Matteo Bortolazzo


1 Answers

Add a do-nothing Initialize static method to your HtmlLabelRenderer class to insure your renderer types are loaded before Forms

Example:

public static void Initialize()
{
}

Usage:

In your AppDelegate before Forms.Init(), call your Initialize method:

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);
}
like image 137
SushiHangover Avatar answered Sep 30 '22 06:09

SushiHangover