我试图在后面的代码中添加WKWebView组件.在iPhone模拟器中构建和部署并运行后,只有白色空白页,除此之外没有其他内容.我做错了什么.下面是我的代码:
using System;
using WebKit;
using UIKit;
using Foundation;
namespace bbbb
{
public partial class ViewController : UIViewController
{
WKWebView wk = null;
protected ViewController(IntPtr handle) : base(handle)
{
// Note: this .ctor should not contain any initialization logic.
}
public override void LoadView()
{
base.LoadView();
NSCoder nc = new NSCoder();
wk = new WKWebView(nc);
View.Add(wk);
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
string url = "https://www.apple.com";
NSUrl nSUrl = new NSUrl(url);
NSUrlRequest urlRequest = new NSUrlRequest(nSUrl);
wk.LoadRequest(urlRequest);
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}
}
解决方法:
NCCoder构造函数用于存档/序列化(即情节提要/ xib实例化).
使用接受框架(CGRect)和WKWebViewConfiguration的构造函数.
例:
var webView = new WKWebView(View.Frame, new WKWebViewConfiguration());
Add(webView);
using (var url = NSUrl.FromString("https://xamarin.com"))
using (var request = NSUrlRequest.FromUrl(url))
{
webView.LoadRequest(request);
}