C# WKWebView使用LoadFileUrl加载本地内容
我试图用LoadFileUrl方法在WKWebView中加载本地html文件,但得到的只是一个空白视图。这是一款Xamarin.Mac应用程序(还没有沙盒) 使用“LoadRequest”从远程服务器加载网页效果很好 “Index.html”文件的生成操作是“BundleResource”C# WKWebView使用LoadFileUrl加载本地内容,c#,xamarin,wkwebview,xamarin.mac,C#,Xamarin,Wkwebview,Xamarin.mac,我试图用LoadFileUrl方法在WKWebView中加载本地html文件,但得到的只是一个空白视图。这是一款Xamarin.Mac应用程序(还没有沙盒) 使用“LoadRequest”从远程服务器加载网页效果很好 “Index.html”文件的生成操作是“BundleResource” 谢谢你的帮助 克里斯·哈蒙斯在地图上给我指出了正确的方向。 只是改变 string index = Path.Combine (NSBundle.MainBundle.BundlePath, "WebApp/
谢谢你的帮助 克里斯·哈蒙斯在地图上给我指出了正确的方向。 只是改变
string index = Path.Combine (NSBundle.MainBundle.BundlePath, "WebApp/Index.html");
string webAppFolder = Path.Combine (NSBundle.MainBundle.BundlePath, "WebApp");
到
我成功了 对于那些不使用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)
}
}
在html文件中,使用如下文件路径
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
不是这样的
<link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
示例目录
@Gusman谢谢,但我已经看到了链接,如果我没有弄错的话,“LoadFileUrl”应该是解决方案,但它不起作用!埃尔卡皮坦的马车还开着吗?我应该复制“temp”文件夹中的html文件并从那里加载吗?应该加载,最好查看webview检查器并查看加载时是否有任何错误:谢谢!您的示例代码和屏幕截图帮助我调试了一个问题。您可以选择您的答案作为解决方案:-)
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">