OGeek|极客世界-中国程序员成长平台

标题: c# - 将本地 CSS 文件加载到 Xamarin IOS WebView 中? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 12:54
标题: c# - 将本地 CSS 文件加载到 Xamarin IOS WebView 中?

我可以像这样将本地 HTML 文件加载到我的 WebView 中(这很好用):

var fileName = "Views/Default.html"; 
var localHtmlUrl = Path.Combine(NSBundle.MainBundle.BundlePath, fileName);
var url = new NSUrl(localHtmlUrl, false);
var request = new NSUrlRequest(url);
WebView.LoadRequest(request);

我想在我的 HTML 文件中引用一个 CSS 文件(也是本地的):

<link href="Content/style.css" rel="stylesheet">

CSS 文件确实存在于 Content 文件夹中,构建操作设置为该文件的内容。

如何通过我的 html 文件引用/加载有问题的 CSS 文件?可能吗?

更新:将 css 和 html 放在不同的文件夹中。将它们都放在 Content 文件夹中,然后更新使用 LoadRequest 时解决问题的 hrefs。



Best Answer-推荐答案


检查此链接 https://developer.xamarin.com/recipes/ios/content_controls/web_view/load_local_content/

部分附加信息

代码中生成的Html也可以显示出来,这对于自定义内容很有用。要显示 Html 字符串,请使用 LoadHtmlString 方法而不是 LoadRequest。将路径传递给 Content 目录有助于 Web View 解析 Html 中的相关 Url,例如链接、图像、CSS 等。

// assumes you've placed all your files in a Content folder inside your app
string contentDirectoryPath = Path.Combine (NSBundle.MainBundle.BundlePath, "Content/");
string html = "<html><a href='Home.html'>Click me</a></html>";
webView.LoadHtmlString(html, new NSUrl(contentDirectoryPath, true));

关于c# - 将本地 CSS 文件加载到 Xamarin IOS WebView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42078911/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4