locked
Implementing IUriToStreamResolver.UriToStreamAsync and using it in WebView RRS feed

  • Question

  • I am using a WebView to load local content and handle those requests myself. I have implemented IUriToStreamResolver:

     public class MyUriToStreamResolver : IUriToStreamResolver    
    {        
        public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
        {            
             Debug.WriteLine(uri.ToString());            
             //process uri and return       
        }    
    }

    And in my MainPage constructor, I create a uri to a TestPage and call NavigateToLocalStreamUri:

    MyUriToStreamResolver resolver = new MyUriToStreamResolver();
    Uri uri2 = new Uri("ms-appdata:///local/TestPage.html");
    AW_WebView.NavigateToLocalStreamUri(uri2, resolver);

    However when I do this, I run into this error: 

    System.ArgumentException: Value does not fall within the expected range.
       at Windows.UI.Xaml.Controls.WebView.NavigateToLocalStreamUri(Uri source, IUriToStreamResolver streamResolver)
       at WebViewTest.MainPage..ctor() in c:\Users\Test\Documents\Visual Studio 2013\Projects\WebViewTest\WebViewTest\MainPage.xaml.cs:line 35

    I'm not exactly sure why this is happening and what I could do to fix it. Any help is appreciated, thank you!

    Tuesday, July 16, 2013 2:38 PM

Answers

  • Figured it out, instead of creating a new Uri object your self, you would have to call WebView's BuildLocalStreamUri method. 

    AW_WebView.BuildLocalStreamUri("MyTag", "TestPage.html");


    • Marked as answer by Ryan.dn Tuesday, July 16, 2013 2:50 PM
    Tuesday, July 16, 2013 2:50 PM

All replies

  • Figured it out, instead of creating a new Uri object your self, you would have to call WebView's BuildLocalStreamUri method. 

    AW_WebView.BuildLocalStreamUri("MyTag", "TestPage.html");


    • Marked as answer by Ryan.dn Tuesday, July 16, 2013 2:50 PM
    Tuesday, July 16, 2013 2:50 PM
  • For local content you don't need your own resolver as well -- is that what you are doing?

    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)

    Tuesday, July 16, 2013 3:10 PM
  • I believe, for local content, you would just be able to refer to it in the source property? 

    I was just using TestPage.html as a sample to see how it works.

    Tuesday, July 16, 2013 4:01 PM
  • Tim,

    I have created my own stream resolver but it doesn't render anything - I need to modify the storage file in memory before returning the stream.  What am I doing wrong here?

            private async Task<IInputStream> GetSampleTextAsync(System.Uri uri)
            {
                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);

                var text = await FileIO.ReadTextAsync(file);

                // I need to modify the text here

                // do not dispose of the memory stream
                var memoryStream = new MemoryStream();

                var writer = new StreamWriter(memoryStream);

                await writer.WriteAsync(text);
                await writer.FlushAsync();

                memoryStream.Position = 0;

                var inputStream = memoryStream.AsInputStream();

                return inputStream;
            }


    Michael S. Scherotter
    Principal Architect Evangelist Microsoft Corporation,
    Blog, Twitter, Facebook

    Monday, October 14, 2013 5:11 PM