Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

javascript - Display local html page in UWP WebView

I would like to display my local html page in a WebView. If I put my html page in the Application's local folder then I can point to it using the following code and it works:

webView.Navigate(new Uri("ms-appdata:///local/myHtmlPage.html"));

But I want to know if there is an equivalent Uri that takes my to the publisher cache folder? The reason I ask is I want to share the html page and it's data (files) between the applications I deploy. I have tried putting the html page in the publisher cache folder, then pointing the WebView's Navigate method to the html page's path but I can't get the WebView to open the page. I get some HRESULT exception with no details as to why it failed.

I want to open files in the html page without prompting the user to pick one and the only way I know how to do this is if the files are in a sub directory of the page's directory. I'll have a javascript function that opens the file using the path and I'll call this function from my UWP application and pass it the name of the file.

Help in this regard would be greatly appreciated.

question from:https://stackoverflow.com/questions/65879709/display-local-html-page-in-uwp-webview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Display local html page in UWP WebView

Sure, you could use StreamUriWinRTResolver to converter html file where in publisher folder to stream. And use WebView NavigateToLocalStreamUri to load that stream.

For example

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");

        }
    }
}

Usage

Uri url = MyWebView.BuildLocalStreamUri("MyTag", "ContentPage.html");
StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver();
MyWebView.NavigateToLocalStreamUri(url, myResolver);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...