加载页面相对于本地文件夹

本文关键字:文件夹 相对于 面相 加载 | 更新日期: 2023-09-27 18:17:44

我试图在UWP应用程序中显示一个相对于本地文件夹的网页。这意味着,我将页面本身作为字符串,但资产(图像,脚本)位于应用程序的本地文件夹中。

我尝试使用NavigateToString方法,根据我的理解应该服务于这项任务,结合WebView控制的Source -属性。

var baseUri = new Uri("ms-appdata:///local/");
WebView.Source = baseUri;
var page = ...;
WebView.NavigateToString(page);

然而,当分配web视图的Source -属性时,这给了我以下错误信息:

Exception thrown: 'System.ArgumentException' in App.exe
Additional information: Value does not fall within the expected range.

所以显然,使用本地应用文件夹的资产是不支持的?

该应用程序的最低目标是Windows 10 Build 10586。

有人知道是否有一个有用的解决方案吗?

编辑:我试着做一个最小的例子。在下面,我创建了一个小的测试应用程序,它什么都不做,只是从本地存储加载一个静态html文件。据我所知,这段代码与XamlWebView示例完全相似,但确实引发了一个ArgumentException。

MainPage.xaml

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <WebView Name="WebView" />
    </Grid>
</Page>

MainPage.xaml.cs

using System;
using System.IO;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            Initialize();
        }
        private async void Initialize()
        {
            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("index.html", CreationCollisionOption.ReplaceExisting);
            using (var sw = new StreamWriter(await file.OpenStreamForWriteAsync()))
            {
                sw.WriteLine("<html>");
                sw.WriteLine("<body>");
                sw.WriteLine("This is some text");
                sw.WriteLine("</body>");
                sw.WriteLine("</html>");
            }
            WebView.Navigate(new Uri("ms-appdata:///local/index.html"));
        }
    }
}

我甚至不知道样品的差异在哪里,找不到任何

加载页面相对于本地文件夹

WebView。Source属性用于获取或设置要在WebView控件中显示的HTML内容的统一资源标识符(Uniform Resource Identifier, URI)源。我们通常在XAML中设置Source属性。XAML解析器自动将字符串转换为Uri。Source属性可以在代码中设置,但不是这样做,我们通常使用导航方法之一,如导航,NavigateToString和NavigateWithHttpRequestMessage方法来加载代码中的内容。

你在代码中使用的URI不是WebView控件的有效URI,所以你得到了一个Value does not fall within the expected range.错误。要从app的本地存储中加载内容,我们需要将内容放在本地文件夹下的子文件夹中。参考备注在WebView类:

要从应用程序的LocalFolderTemporaryFolder数据存储中加载未压缩和未加密的内容,请使用Navigate方法和使用ms-appdata方案Uri方法。WebView对该方案的支持要求您将内容放在本地或临时文件夹下的子文件夹中。这样就可以导航到这样的uri: ms-appdata:///local/folder/file.html和ms-appdata:///temp/folder/file.html。(要加载压缩或加密文件,请参见NavigateToLocalStreamUri)

这些第一级子文件夹中的每一个都与其他第一级子文件夹中的内容隔离。例如,你可以导航到ms-appdata:///temp/folder1/file.html,但你不能在这个文件中有一个链接到ms-appdata:///temp/folder2/file.html。但是,你仍然可以使用ms-appx-web方案链接到应用包中的HTML内容,也可以使用httphttps URI方案链接到web内容。

所以当你使用WebView.Navigate(new Uri("ms-appdata:///local/index.html"));,你仍然得到错误作为ms-appdata:///local/index.html也不是WebView的有效URI。

此外,尽管NavigateToString支持引用外部文件(如CSS、脚本、图像和字体)的内容。但是它只支持应用包中使用ms-appx-web方案的内容,以及使用httphttps URI方案的web内容。它不能与位于app本地文件夹中的资源一起工作。为了实现您想要的,您需要将这些资源放入本地文件夹下的子文件夹中,然后将html字符串存储到同一子文件夹下的文件中。在html中,您可以使用相对URI来引用这些资产,就像我之前的回答一样。