【问题标题】:In Xamarin, how to handle the WKWebView ShouldStartLoad event在 Xamarin 中,如何处理 WKWebView ShouldStartLoad 事件
【发布时间】:2014-09-30 14:51:59
【问题描述】:

使用 UIWebView 来拦截 ShouldStartLoad 事件,我所要做的就是:

_webView.ShouldStartLoad += (webView, request, navigationType) => { return true }

如何使用 WKWebView 处理这个问题?

【问题讨论】:

    标签: ios xamarin wkwebview


    【解决方案1】:

    您需要在 WKNavigationDelegate 子类中覆盖 DecidePolicy

    public class WebNavigationDelegate : WKNavigationDelegate
    {
    
        ...
    
        public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
        {
            var url = navigationAction.Request.Url;
            if (true) //Whatever your test happens to be
            {
                decisionHandler(WKNavigationActionPolicy.Allow);
            }
            else
            {
                decisionHandler(WKNavigationActionPolicy.Cancel);
            }
        }
    
        ...
    
    }
    

    然后将 webview 的导航委托设置为您的新类。

    _webView.NavigationDelegate = new WebNavigationDelegate(this);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 2018-02-06
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      相关资源
      最近更新 更多