WKWebView使用LoadFileUrl加载本地内容

fre*_*aso 3 c# xamarin xamarin.mac wkwebview

我正在尝试使用LoadFileUrl方法在WKWebView中加载本地html文件,但我得到的只是一个空白视图.这是一个Xamarin.Mac应用程序(还没有Sandbox).

WKWebViewConfiguration conf = new WKWebViewConfiguration();
WKWebView www = new WKWebView (View.Frame, conf);
View = www;

string index = Path.Combine (NSBundle.MainBundle.BundlePath, "WebApp/Index.html");
string webAppFolder = Path.Combine (NSBundle.MainBundle.BundlePath, "WebApp");

www.LoadFileUrl (new NSUrl ("file://"+index), new NSUrl ("file://"+webAppFolder));
Run Code Online (Sandbox Code Playgroud)

使用"LoadRequest"从远程服务器加载网页工作得很好.

"Index.html"文件的构建操作是"BundleResource"

感谢你的帮助!

Mar*_* T. 5

对于那些不使用Xamarin并与loadFileURL战斗的人:几个小时(像我一样).

将Web文件夹移动到项目时,选择"创建文件夹引用"

在此输入图像描述

然后使用类似这样的代码:

if let filePath = NSBundle.mainBundle().resourcePath?.stringByAppendingString("/WebApp/index.html"){
  let url = NSURL(fileURLWithPath: filePath)
  if let webAppPath = NSBundle.mainBundle().resourcePath?.stringByAppendingString("/WebApp") {
    let webAppUrl = NSURL(fileURLWithPath: webAppPath, isDirectory: true)
    webView.loadFileURL(url, allowingReadAccessToURL: webAppUrl)
  }
}
Run Code Online (Sandbox Code Playgroud)

在html文件中使用像这样的文件路径

<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

不喜欢这个

<link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

示例目录 在此输入图像描述