c# - 在 Xamarin.Forms Web 视图中打开本地 HTML 文件而不是在资产文件夹中
问题描述
我正在尝试打开一个多页 HTML 网页(即 test.html、test.css 和 test.js),该网页已动态下载(因此无法使用资产)并存储在应用程序内部存储中(Xamarin.Essentials.FileSystem .AppDataDirectory)。
我尝试使用的 URL 如下:
file:///data/user/0/com.test/files/HTML/Test.html
但是我只是找不到文件。
var filesToDownload = new string[] { "http://myserver/test/test.html", "http://myserver/test/test.css" };
var directory = System.IO.Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory, "HTML");
if (System.IO.Directory.Exists(directory))
{
System.IO.Directory.Delete(directory, true);
}
System.IO.Directory.CreateDirectory(directory);
using (var wc = new System.Net.WebClient())
{
foreach (var f in filesToDownload)
{
wc.DownloadFile(f, System.IO.Path.Combine(directory, System.IO.Path.GetFileName(f)));
}
}
var source = new UrlWebViewSource
{
Url = System.IO.Path.Combine("file://" + directory, "Test.html")
};
WebView1.Source = source;
解决方案
您必须确保以下几点:
1 将Xamarin.Essentials NuGet包添加到每个项目
2http://myserver/test/test.html
并且http://myserver/test/test.css
存在且可访问。
3 webview源的文件名应该和你写入存储的文件名一致。不是Test.html
,而是test.html
所以
Url = System.IO.Path.Combine("file://" + directory, "Test.html")
应该修改为
Url = System.IO.Path.Combine("file://" + directory, "test.html")
推荐阅读
- java - 检查用户是否来自加利福尼亚
- java - 线程“主”java.lang.IllegalArgumentException 中的异常:在 META-INF/spring.factories 中找不到自动配置类
- c# - C# WPF 组合框 DISplayMemberPath 不显示
- java - 可以使用用户名密码流访问使用 EWS 的交换收件箱吗?
- pdf - 带有 TSA 时间戳的 PDF 签名
- javascript - Javascript - 在每个第 4 个元素之后将对象数组拆分为多个数组
- html-email - 电子邮件:图像和文本对齐问题
- android - 我应该在使用 MediaCodec 解码期间使用 ImageReader 来访问 Image
- java - 检索成员详细信息后添加成功响应
- r - 在数据集中按行选择顺序数据的函数