在Xamarin IO中实现在一个WebViewRenderer中接收的错误处理和脚本消息

时间:2016-04-25 07:22:19

标签: xamarin xamarin.ios xamarin.forms

现在我有网页视图渲染器的代码,如:

 public sealed class MyWebViewRenderer : ViewRenderer<WebView, WKWebView>, IWKScriptMessageHandler
    {
        private const string ScriptMessageHandlerName = "native";
        private WKUserContentController userController;
        private WKWebView m_NativeView;

        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);

            var visualWebView = (ImWebView)Element;

            if (Control == null && e.NewElement != null)
            {

                this.userController = new WKUserContentController();
                var config = new WKWebViewConfiguration
                {
                    UserContentController = this.userController
                };

                this.userController.AddScriptMessageHandler(this, ScriptMessageHandlerName);

                m_NativeView = new WKWebView(this.Frame, config) { WeakNavigationDelegate = new ImNavigationDelegate()};


                SetNativeControl(m_NativeView);
    }

        public void DidReceiveScriptMessage(WKUserContentController userContentController, WKScriptMessage message)
        {
        ...
        }
}

这个类允许我从JS接收消息。但现在我想收到页面加载失败的消息。我找到了唯一的方法 - 使用Xamarin.Forms.Platform.iOS.WebViewRenderer作为渲染器的基类并使用

public sealed class MyWebViewRenderer : Xamarin.Forms.Platform.iOS.WebViewRenderer
 {

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        var visualWebView = (ImWebView)Element;

            if (this.ViewController == null && e.NewElement != null)
            {
                base.Delegate = new CustomNavigationDelegate() { Control = visualWebView };
        }

            ...
     }



    public class CustomNavigationDelegate : UIWebViewDelegate
    {
        public ImWebView Control { get; set; }

        public override void LoadFailed(UIWebView webView, NSError error)
        {
          ...
        }
    }
 }

加载失败捕获工作正常,但是使用此代码我无法从JS获取消息。 有没有办法从一个类中获取JS代码中的LoadFailed事件和消息?

1 个答案:

答案 0 :(得分:0)

我建议您继续使用从WKWebView继承的渲染器。当导航时出现错误时,我们可以在DidFailNavigation中使用WKNavigationDelegate虚拟方法来获取回调。你可以像下面那样使用它

public sealed class MyWebViewRenderer : ViewRenderer<WebView, WKWebView>, IWKScriptMessageHandler
{
    private const string ScriptMessageHandlerName = "native";
    private WKUserContentController userController;
    private WKWebView m_NativeView;

    protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
    {
        base.OnElementChanged(e);

        var visualWebView = (ImWebView)Element;

        if (Control == null && e.NewElement != null)
        {

            this.userController = new WKUserContentController();
            var config = new WKWebViewConfiguration
            {
                UserContentController = this.userController
            };

            this.userController.AddScriptMessageHandler(this, ScriptMessageHandlerName);

            m_NativeView = new WKWebView(this.Frame, config) { WeakNavigationDelegate = new NavigationDelegate()};


            SetNativeControl(m_NativeView);
        }
    }

        public void DidReceiveScriptMessage(WKUserContentController userContentController, WKScriptMessage message)
        {

        }
}
public class NavigationDelegate : WKNavigationDelegate
{

    public override void DidFailNavigation( WKWebView webView, WKNavigation navigation, NSError error )
    {
        // If navigation fails, this gets called
    }
}