c#-Xamarin iOS,在代码中添加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.
        }
    }
}

解决方法:

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);
}   
上一篇:Flutter 与 iOS 原生 WebView 对比


下一篇:JavaScript 简介及引入方式