【问题标题】:Custom renderer doesn't work in iOS + library自定义渲染器在 iOS + 库中不起作用
【发布时间】:2017-05-30 11:09:19
【问题描述】:

我不知道为什么我的 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();
        }

如果进入项目,它可以在 Android、UWP 和 iOS 上正常工作。

https://github.com/matteobortolazzo/HtmlLabelPlugin

【问题讨论】:

    标签: ios forms xamarin custom-renderer


    【解决方案1】:

    向您的HtmlLabelRenderer 类添加一个无操作的Initialize 静态方法,以确保您的渲染器类型在表单之前加载

    示例:

    public static void Initialize()
    {
    }
    

    用法:

    在你的AppDelegate 之前 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);
    }
    

    【讨论】:

    • 谢谢!它有效:) 由于UWP,我发现了另一个问题,PCL 丢失了。 Android 没有它也能工作,但 iOS 和 UWP 不行。
    • 是的,你在 android 上不需要这个
    猜你喜欢
    • 2019-11-15
    • 2013-07-17
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多