在Xamarin.Forms中打开本地HTML文件不在资产文件夹中查看

问题描述 投票:0回答:1

我试图打开一个多页面的HTML网页(即test.html,test.css和test.js),它已被动态下载(因此无法使用资产)并存储在应用程序内部存储中(Xamarin.Essentials.FileSystem) .AppDataDirectory)。

我尝试使用的URL如下:

文件:///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;
c# xamarin.forms xamarin.forms.webview
1个回答
0
投票

您必须确保以下几点:

1将Xamarin.Essentials NuGet包添加到每个项目中

2 http://myserver/test/test.htmlhttp://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")

© www.soinside.com 2019 - 2024. All rights reserved.