WebView.BuildLocalStreamUri(String, String) 메서드

정의

NavigateToLocalStreamUri에 전달할 수 있는 URI를 만듭니다.

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

루트를 기준으로 하는 리소스의 경로입니다.

반환

contentIdentifierrelativePath를 결합하고 정규화하여 만든 URI입니다.

예제

다음 코드 예제에서는 앱 패키지에서 파일을 제공할 확인자에서 이 메서드를 사용하는 방법을 보여 있습니다. 전체 예제는 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"); }
    }
}

적용 대상

추가 정보