Xamarin iOS,在代码中添加WKWebView,部署后没有任何反应

时间:2018-03-07 05:24:46

标签: c# ios xamarin wkwebview

我正在尝试在代码中添加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.
        }
    }
}

1 个答案:

答案 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);
}