EN
本內容沒有您的語言版本,但有英文版本。

WebView.NavigateToLocalStreamUri method

Applies to Windows and Windows Phone

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

Syntax


public void NavigateToLocalStreamUri(
  Uri source, 
  IUriToStreamResolver streamResolver
)

Parameters

source

Type: System.Uri [.NET] | Windows::Foundation::Uri [C++]

A URI identifying the local HTML content to load.

streamResolver

Type: IUriToStreamResolver

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

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.

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


Requirements

Minimum supported client

Windows 8.1

Minimum supported server

Windows Server 2012 R2

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml.Controls
Windows::UI::Xaml::Controls [C++]

Metadata

Windows.winmd

See also

WebView
IUriToStreamResolver
BuildLocalStreamUri
XAML WebView control sample

 

 

顯示:
© 2014 Microsoft