我试图在 WKWebView 中加载一些网页。我想在加载webview时在屏幕上显示加载微调器,并在页面加载后隐藏它(特别是当 DidFinishNavigation 被调用时),但 DidFinishNavigation 永远不会被调用正在加载的页面包含HTML中的iframe元素,因此,我无法隐藏加载微调器。
请让我知道为什么会发生这种情况,以及是否有解决此问题的方法。
例如:
网络代码
<section id="Document">
<iframe src="SomeOtherPages"></iframe>
<div id="Loader"></div>
</section>
XAMARIN代码
public override void ViewDidLoad()
{
base.ViewDidLoad();
var userController = new WKUserContentController();
// add JS function to the document so we can call it from C#
userController.AddUserScript(
new WKUserScript(
new NSString(Global.MessageHandlerFunction),
WKUserScriptInjectionTime.AtDocumentEnd, false));
// register messageHandler 'native' that can be called with window.webkit.messageHandlers.native
userController.AddScriptMessageHandler(this, "native");
var config = new WKWebViewConfiguration { UserContentController = userController };
webView = new WKWebView(View.Frame, config) { WeakNavigationDelegate = this };
webView.AllowsBackForwardNavigationGestures = true;
webView.UIDelegate = this;
webView.NavigationDelegate = this;
View.AddSubview(webView);
this.loadingOverlay = new LoadingOverlay(View.Frame);
View.AddSubview(loadingOverlay);
webView.LoadRequest(new NSUrlRequest(new NSUrl(defaultUrl)));
}
[Export("webView:decidePolicyForNavigationAction:decisionHandler:")]
public void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
{
decisionHandler(WKNavigationActionPolicy.Allow);
Console.WriteLine("DecidePolicy " + webView.Url);
SetLoadingOverlay(false);
}
public void SetLoadingOverlay(bool hide){
InvokeOnMainThread(()=>{
UIView.Animate(0.5, () => { loadingOverlay.Hidden = hide; });
});
}
[Export("webView:didFinishNavigation:")]
public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
Console.WriteLine("Finish Loading...");
SetLoadingOverlay(true);
}
[Export("webView:didFailNavigation:withError:")]
public void DidFailNavigation(WKWebView webView, WKNavigation navigation, NSError error)
{
Console.WriteLine("Failed Loading...");
SetLoadingOverlay(true);
}
[Export("webView:didFailProvisionalNavigation:withError:")]
public void DidFailProvisionalNavigation(WKWebView webView, WKNavigation navigation, NSError error)
{
Console.WriteLine("Failed Loading...");
SetLoadingOverlay(true);
}