我正在尝试在代码中添加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.
}
}
}
答案 0 :(得分:4)
NCCoder
构造函数用于存档/序列化(即Storyboard / 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);
}