Xamarin iOS WKWebView DidFinishNavigation 和 DidStartProvisionalNavigation 未被调用
Xamarin iOS WKWebView DidFinishNavigation and DidStartProvisionalNavigation not getting called
我正在 iOS Xamarin 中实现 WKWebView,我想在加载开始和加载完成时做一些事情。我已经实现了 IWKNavigationDelegate 并添加了以下函数,但没有一个被调用。我是不是漏了什么
Class定义
public partial class MasterViewController : UIViewController,IScanSuccessCallback, IWKScriptMessageHandler, IWKNavigationDelegate
{ }
导航委托函数
public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
Console.WriteLine ("DidFinishNavigation");
}
public void DidFailNavigation( WKWebView webView, WKNavigation navigation, NSError error )
{
// If navigation fails, this gets called
Console.WriteLine("DidFailNavigation");
}
public void DidFailProvisionalNavigation( WKWebView webView, WKNavigation navigation, NSError error )
{
// If navigation fails, this gets called
Console.WriteLine("DidFailProvisionalNavigation");
}
public void DidStartProvisionalNavigation( WKWebView webView, WKNavigation navigation )
{
// When navigation starts, this gets called
Console.WriteLine("DidStartProvisionalNavigation");
}
您需要将 NavigationDelegate class 显式分配给 webview
WKWebView web = new WKWebView();
// if the current class implements IWKNavigationDelegate, you can do this
web.NavigationDelegate = this;
// or you can create a separate class that implements IWKNavigationDelegate
web.NavigationDelegate = new MyWebDelegate();
补充 Jason 的回答。
您缺少可选委托方法所需的 ExportAttribute
,当 Foo
是 ObjC 委托时,Xamarin Studio 自动完成引擎应在您每次实现任何 IFooInterface
时为您生成此内容然后你输入 override
(它应该列出可选的而不是可选的成员)。 ExportAttribute
仅对可选成员(c# 接口中的非抽象成员)是必需的。
在这种特殊情况下,WKNavigationDelegate
ObjC 协议的所有成员都是可选的,因此您需要 Export
属性和 ObjC 选择器作为其参数。
您需要在 using 语句中添加 using Foundation
,ExportAttribute
就在其中。
[Export ("webView:didFinishNavigation:")]
public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
Console.WriteLine ("DidFinishNavigation");
}
[Export ("webView:didFailNavigation:withError:")
public void DidFailNavigation( WKWebView webView, WKNavigation navigation, NSError error )
{
// If navigation fails, this gets called
Console.WriteLine("DidFailNavigation");
}
[Export ("webView:didFailProvisionalNavigation:withError:")]
public void DidFailProvisionalNavigation( WKWebView webView, WKNavigation navigation, NSError error )
{
// If navigation fails, this gets called
Console.WriteLine("DidFailProvisionalNavigation");
}
[Export ("webView:didStartProvisionalNavigation:")]
public void DidStartProvisionalNavigation( WKWebView webView, WKNavigation navigation )
{
// When navigation starts, this gets called
Console.WriteLine("DidStartProvisionalNavigation");
}
希望这对您有所帮助。
我正在 iOS Xamarin 中实现 WKWebView,我想在加载开始和加载完成时做一些事情。我已经实现了 IWKNavigationDelegate 并添加了以下函数,但没有一个被调用。我是不是漏了什么
Class定义
public partial class MasterViewController : UIViewController,IScanSuccessCallback, IWKScriptMessageHandler, IWKNavigationDelegate
{ }
导航委托函数
public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
Console.WriteLine ("DidFinishNavigation");
}
public void DidFailNavigation( WKWebView webView, WKNavigation navigation, NSError error )
{
// If navigation fails, this gets called
Console.WriteLine("DidFailNavigation");
}
public void DidFailProvisionalNavigation( WKWebView webView, WKNavigation navigation, NSError error )
{
// If navigation fails, this gets called
Console.WriteLine("DidFailProvisionalNavigation");
}
public void DidStartProvisionalNavigation( WKWebView webView, WKNavigation navigation )
{
// When navigation starts, this gets called
Console.WriteLine("DidStartProvisionalNavigation");
}
您需要将 NavigationDelegate class 显式分配给 webview
WKWebView web = new WKWebView();
// if the current class implements IWKNavigationDelegate, you can do this
web.NavigationDelegate = this;
// or you can create a separate class that implements IWKNavigationDelegate
web.NavigationDelegate = new MyWebDelegate();
补充 Jason 的回答。
您缺少可选委托方法所需的 ExportAttribute
,当 Foo
是 ObjC 委托时,Xamarin Studio 自动完成引擎应在您每次实现任何 IFooInterface
时为您生成此内容然后你输入 override
(它应该列出可选的而不是可选的成员)。 ExportAttribute
仅对可选成员(c# 接口中的非抽象成员)是必需的。
在这种特殊情况下,WKNavigationDelegate
ObjC 协议的所有成员都是可选的,因此您需要 Export
属性和 ObjC 选择器作为其参数。
您需要在 using 语句中添加 using Foundation
,ExportAttribute
就在其中。
[Export ("webView:didFinishNavigation:")]
public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
Console.WriteLine ("DidFinishNavigation");
}
[Export ("webView:didFailNavigation:withError:")
public void DidFailNavigation( WKWebView webView, WKNavigation navigation, NSError error )
{
// If navigation fails, this gets called
Console.WriteLine("DidFailNavigation");
}
[Export ("webView:didFailProvisionalNavigation:withError:")]
public void DidFailProvisionalNavigation( WKWebView webView, WKNavigation navigation, NSError error )
{
// If navigation fails, this gets called
Console.WriteLine("DidFailProvisionalNavigation");
}
[Export ("webView:didStartProvisionalNavigation:")]
public void DidStartProvisionalNavigation( WKWebView webView, WKNavigation navigation )
{
// When navigation starts, this gets called
Console.WriteLine("DidStartProvisionalNavigation");
}
希望这对您有所帮助。