WebView.BuildLocalStreamUri method

Applies to Windows and Windows Phone

Creates a URI that you can pass to NavigateToLocalStreamUri.

Syntax


Public Function BuildLocalStreamUri(
  contentIdentifier As String,  
  relativePath As String 
) As Uri

Parameters

contentIdentifier

Type: System.String [.NET] | Platform::String [C++]

A unique identifier for the content the URI is referencing. This defines the root of the URI.

relativePath

Type: System.String [.NET] | Platform::String [C++]

The path to the resource, relative to the root.

Return value

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

The URI created by combining and normalizing the contentIdentifier and relativePath.

Examples

The following code example shows how to use this method with 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.GetInputStreamAt(0);
        }
        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
NavigateToLocalStreamUri
XAML WebView control sample

 

 

Show:
© 2014 Microsoft