WebView.BuildLocalStreamUri(String, String) Метод

Определение

Создает универсальный код ресурса (URI), который можно передать в NavigateToLocalStreamUri.

public:
 virtual Uri ^ BuildLocalStreamUri(Platform::String ^ contentIdentifier, Platform::String ^ relativePath) = BuildLocalStreamUri;
Uri BuildLocalStreamUri(winrt::hstring const& contentIdentifier, winrt::hstring const& relativePath);
public System.Uri BuildLocalStreamUri(string contentIdentifier, string relativePath);
function buildLocalStreamUri(contentIdentifier, relativePath)
Public Function BuildLocalStreamUri (contentIdentifier As String, relativePath As String) As Uri

Параметры

contentIdentifier
String

Platform::String

winrt::hstring

Уникальный идентификатор содержимого, на который ссылается URI. Это определяет корень универсального кода ресурса (URI).

relativePath
String

Platform::String

winrt::hstring

Путь к ресурсу относительно корня.

Возвращаемое значение

URI, созданный путем объединения и нормализации contentIdentifier и relativePath.

Примеры

В следующем примере кода показано, как использовать этот метод с сопоставительом, который будет обслуживать файл из пакета приложения. Полный пример см. в примере элемента управления XAML WebView.

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.GetInputStreamAt(0);
        }
        catch (Exception) { throw new Exception("Invalid path"); }
    }
}

Применяется к

См. также раздел