在 UWP WebView 中显示本地 html 页面
Display local html page in UWP WebView
我想在 WebView 中显示我的本地 html 页面。如果我将我的 html 页面放在应用程序的本地文件夹中,那么我可以使用以下代码指向它并且它有效:
webView.Navigate(new Uri("ms-appdata:///local/myHtmlPage.html"));
但我想知道是否存在将我带到发布者缓存文件夹的等效 URI?我问的原因是我想在我部署的应用程序之间共享 html 页面及其数据(文件)。我尝试将 html 页面放入发布者缓存文件夹,然后将 WebView 的导航方法指向 html 页面的路径,但我无法让 WebView 打开该页面。我收到一些 HRESULT 异常,但没有详细说明它失败的原因。
我想在 html 页面中打开文件而不提示用户选择一个,我知道如何做到这一点的唯一方法是文件位于页面目录的子目录中。我将有一个使用路径打开文件的 javascript 函数,我将从我的 UWP 应用程序调用此函数并将文件名传递给它。
在这方面的帮助将不胜感激。
Display local html page in UWP WebView
当然,您可以使用 StreamUriWinRTResolver
将发布者文件夹中的 html 文件转换为流式传输。并使用 WebView NavigateToLocalStreamUri
加载该流。
例如
public sealed class StreamUriWinRTResolver : IUriToStreamResolver
{
/// <summary>
/// The entry point for resolving a Uri to a stream.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
{
if (uri == null)
{
throw new Exception();
}
string path = uri.AbsolutePath;
// Because of the signature of this method, it can't use await, so we
// call into a separate helper method that can use the C# await pattern.
return getContent(path).AsAsyncOperation();
}
/// <summary>
/// Helper that maps the path to package content and resolves the Uri
/// Uses the C# await pattern to coordinate async operations
/// </summary>
private async Task<IInputStream> getContent(string path)
{
// We use a package folder as the source, but the same principle should apply
// when supplying content from other locations
try
{
var filename = path.Remove(0, 1);
StorageFile f = await ApplicationData.Current.GetPublisherCacheFolder("Folder1").GetFileAsync(filename);
IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
return stream.GetInputStreamAt(0);
}
catch (Exception e)
{
throw new Exception("Invalid path");
}
}
}
用法
Uri url = MyWebView.BuildLocalStreamUri("MyTag", "ContentPage.html");
StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver();
MyWebView.NavigateToLocalStreamUri(url, myResolver);
我想在 WebView 中显示我的本地 html 页面。如果我将我的 html 页面放在应用程序的本地文件夹中,那么我可以使用以下代码指向它并且它有效:
webView.Navigate(new Uri("ms-appdata:///local/myHtmlPage.html"));
但我想知道是否存在将我带到发布者缓存文件夹的等效 URI?我问的原因是我想在我部署的应用程序之间共享 html 页面及其数据(文件)。我尝试将 html 页面放入发布者缓存文件夹,然后将 WebView 的导航方法指向 html 页面的路径,但我无法让 WebView 打开该页面。我收到一些 HRESULT 异常,但没有详细说明它失败的原因。
我想在 html 页面中打开文件而不提示用户选择一个,我知道如何做到这一点的唯一方法是文件位于页面目录的子目录中。我将有一个使用路径打开文件的 javascript 函数,我将从我的 UWP 应用程序调用此函数并将文件名传递给它。
在这方面的帮助将不胜感激。
Display local html page in UWP WebView
当然,您可以使用 StreamUriWinRTResolver
将发布者文件夹中的 html 文件转换为流式传输。并使用 WebView NavigateToLocalStreamUri
加载该流。
例如
public sealed class StreamUriWinRTResolver : IUriToStreamResolver
{
/// <summary>
/// The entry point for resolving a Uri to a stream.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
{
if (uri == null)
{
throw new Exception();
}
string path = uri.AbsolutePath;
// Because of the signature of this method, it can't use await, so we
// call into a separate helper method that can use the C# await pattern.
return getContent(path).AsAsyncOperation();
}
/// <summary>
/// Helper that maps the path to package content and resolves the Uri
/// Uses the C# await pattern to coordinate async operations
/// </summary>
private async Task<IInputStream> getContent(string path)
{
// We use a package folder as the source, but the same principle should apply
// when supplying content from other locations
try
{
var filename = path.Remove(0, 1);
StorageFile f = await ApplicationData.Current.GetPublisherCacheFolder("Folder1").GetFileAsync(filename);
IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
return stream.GetInputStreamAt(0);
}
catch (Exception e)
{
throw new Exception("Invalid path");
}
}
}
用法
Uri url = MyWebView.BuildLocalStreamUri("MyTag", "ContentPage.html");
StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver();
MyWebView.NavigateToLocalStreamUri(url, myResolver);