Udostępnij za pośrednictwem


WebView.NavigateToLocalStreamUri(Uri, IUriToStreamResolver) Method

Definition

Loads local web content at the specified URI using an IUriToStreamResolver.

public:
 virtual void NavigateToLocalStreamUri(Uri ^ source, IUriToStreamResolver ^ streamResolver) = NavigateToLocalStreamUri;
void NavigateToLocalStreamUri(Uri const& source, IUriToStreamResolver const& streamResolver);
public void NavigateToLocalStreamUri(System.Uri source, IUriToStreamResolver streamResolver);
function navigateToLocalStreamUri(source, streamResolver)
Public Sub NavigateToLocalStreamUri (source As Uri, streamResolver As IUriToStreamResolver)

Parameters

source
Uri Uri

A URI identifying the local HTML content to load.

streamResolver
IUriToStreamResolver

A resolver that converts the URI into a stream to load.

Windows requirements

App capabilities
enterpriseCloudSSO

Examples

The following code example shows how to create and use a resolver that will serve a file from the app package. For a complete example, see the XAML WebView control sample.

public sealed partial class TestPage : Page
{
    // ... other code ...

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // The 'Host' part of the URI for the ms-local-stream protocol needs to be a combination of the package name
        // and an application-defined key, which identifies the specific resolver, in this case 'MyTag'.

        Uri url = webView4.BuildLocalStreamUri("MyTag","/Minesweeper/default.html");
        StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver();

        // Pass the resolver object to the navigate call.
        webView4.NavigateToLocalStreamUri(url, myResolver);
    }
}

public sealed class StreamUriWinRTResolver : IUriToStreamResolver
{
    public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
    {
        if (uri == null)
        {
            throw new Exception();
        }
        string path = uri.AbsolutePath;

        // Because of the signature of the 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();
    }

    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
        {
            Uri localUri= new Uri("ms-appx:///html" + path);
            StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
            IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
            return stream;
        }
        catch (Exception) { throw new Exception("Invalid path"); }
    }
}

Remarks

Use this method to load local content that the NavigateToString method won't handle. NavigateToString provides an easy way to navigate to static HTML content, including content with references to resources such as CSS, scripts, images, and fonts. However, NavigateToString does not provide a way to generate these resources programmatically.

To use the NavigateToLocalStreamUri method, you must pass in an IUriToStreamResolver implementation that translates a URI pattern into a content stream. You can do this to supply the content for all the resources used by a web page, or series of pages. For example, you can use this method to display content saved on the local file system as encrypted files or in cab packages. When the content is requested, you can use an IUriToStreamResolver implementation to decrypt it on the fly.

The IUriToStreamResolver interface has one method, UriToStreamAsync which takes the URI and returns the stream. The URI is in the form of “ms-local-stream://appname_KEY/folder/file” where KEY identifies the resolver. Use BuildLocalStreamUri to create a URI in the correct format that references the local content to load.

Note

Your IUriToStreamResolver implementation must be agile to prevent deadlock that can occur when the UI thread waits for the IUriToStreamResolver to finish its work before continuing. For more info, see Threading and Marshaling

If the app possesses the enterpriseCloudSSO capability, then web pages hosted inside the WebView control can use single sign on with Azure Active Directory (AAD) resources.

Applies to

See also