gpt4 book ai didi

c# - WKWebView 使用 LoadFileUrl 加载本地内容

转载 作者:行者123 更新时间:2023-11-30 13:55:18 24 4
gpt4 key购买 nike

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

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));

使用“LoadRequest”从远程服务器加载网页效果很好。

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

感谢您的帮助!

最佳答案

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

将 web 文件夹移动到项目时,选择“创建文件夹引用”

enter image description here

然后使用如下代码:

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">

示例目录 enter image description here

关于c# - WKWebView 使用 LoadFileUrl 加载本地内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35725905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com