none
How to access a packaged html file in webview control?

    Question

  • I am trying to use the webview control to display a bing or google map using their API scripts.  I understand that there are issues with context that prevent certain types of operations but these issues may be circumvented by using iframes in a parent html document.

    At the moment, I am having trouble understanding how to use a local html file as a source for the webview control.  There are javascript examples which seem to be able to do this with something like src="ms-wwa-web:///TestPage.html" in an iframe attribute.

    I have had no luck.  I can put the html in a string and use: webview.NavigateToString() method and I can set source to an external Uri.  But I can't seem to get access to an html file in my project--even when using the javascript app example of embedding an iframe element into the string version of the document.

    So I would like to know if it is possible to use a local (packaged) file as a source for the webview control in the local or web context.

    Thursday, September 29, 2011 3:53 PM

Answers

  • Hi,

    Unfortunately the WebView control doesn't understand the syntax that is necessary to load a file from your package.  This is a limitation of the developer preview.  We are looking into what it may take to support this.

    Thanks for your input.

    Mike

    Thursday, September 29, 2011 4:50 PM

All replies

  • Hi,

    Unfortunately the WebView control doesn't understand the syntax that is necessary to load a file from your package.  This is a limitation of the developer preview.  We are looking into what it may take to support this.

    Thanks for your input.

    Mike

    Thursday, September 29, 2011 4:50 PM
  • As a work-around, you can put your HTML in the package (as content) and then manually load it into the WebView:

     

          StorageFile     sf = await Package.Current.InstalledLocation.GetFileAsync("TestPage.html");
          uint            length = (uint)sf.Size;
          IInputStream    input = await sf.OpenForReadAsync();
          byte[]          buffer = new byte[length];
    
          await input.ReadAsync(buffer.AsBuffer(), length, InputStreamOptions.None);
    
          string          content = Encoding.GetEncoding("utf-8").GetString(buffer, 0, buffer.Length);
    
          webView.NavigateToString(content);
    
    Joe

    • Proposed as answer by Water Cube Wednesday, February 29, 2012 2:05 AM
    Friday, September 30, 2011 2:10 AM
  • Joe,

    Thank you for posting the workaround to load a file to string then use .NavigateToString().

    Unfortunately, this method does not solve my basic problem.  I want to use the Bing Maps API.  This requires that the html file load an externally sourced script.  The security rules do not allow a 'sting hosted' html file to load an external script.

    There is a Mashup sample written only in javascript which is able to read a local html file that contains an iframe.  The source of the iframe is another local html file that is able to load an external script--and thus use the Bing Maps API.

    It is kind of frustrating to realize there is something that you can do in the javascript domain that you cannot do with C# in winRT.

    Of course, when/if there is a Bing Map control for winRT, this will all be moot.

    In the meantime, my options appear to be:

    1. Write my app using javascript (not a pleasant thought)
    2. Use the REST API and get a static map.
    3. Use the WebViewer to access a server sourced mapviewer that I write (kludge)
    4. Use a map service that allows the control javascript to be embedded in my app. 

     

     

    Friday, September 30, 2011 12:45 PM
  • Hi Joe,

    I was testing this workaround but cannot get the buffer.AsBuffer() to compile. Getting System.Array does not contain a definition for'AsBuffer'

    -- Ed

    Monday, October 31, 2011 4:15 PM
  • hi, Joe, it seems byte[] don't have a method named AsBuffer().


    Tuesday, November 15, 2011 3:41 AM
  • AsBuffer is an extension method. To use it add:

    using System.Runtime.InteropServices.WindowsRuntime;
    

    --Rob

     

    Tuesday, November 15, 2011 5:39 AM
    Owner
  • Maybe like on Windos Phone: to be able to save an HTML file in the isolated local storage and set the Base property of the control to a ILS directory ?
    http://wp7wonders.wordpress.com/
    Tuesday, November 15, 2011 7:19 PM
  • Thx,it works。
    呵,呵呵~~
    Monday, January 09, 2012 7:00 AM
  • Hi,Joe

    When there are no image files in html file, It's right. But When there are many image files, the image can't display. Could you help me? Thank you

    water


    • Edited by Water Cube Friday, March 16, 2012 9:34 AM
    Wednesday, February 29, 2012 2:04 AM
  • Hi Mike,

       It seemed the WebView control still has the issue on the Custom Preview. Will it be supported on the future version?

    Joe

    Friday, March 02, 2012 8:31 AM
  • Hi Mike,

       It seemed the WebView control still has the issue on the Custom Preview. Will it be supported on the future version?

    Joe


    Up
    Monday, March 05, 2012 6:44 AM
  • Double post: see How can I load a HTML-File into Webview for discussion on the Consumer Preview.

    Monday, March 05, 2012 7:18 AM
    Owner
  • Double post: see How can I load a HTML-File into Webview for discussion on the Consumer Preview.

    Hi Rob,

    I've used the method in this post or the other post, but i still cannot load image or navigate to other packaged html files. I've tried all UriSchemes in the Uri, and all of ms-appx, ms-app, ms-wwa and ms-wwa-web, but the best i can get is either an empty page or Navigation to webpage is canceled error. Will the WebView be changed soon?


    • Edited by P.Akial Monday, March 05, 2012 7:40 AM
    Monday, March 05, 2012 7:39 AM
  • Working code for windows RTM

    webView1.Navigate(new Uri("ms-appx-web:///mydir/myfile.html"));

    mydir is a folder in solution file.

    • Proposed as answer by Prateek Jaiswal Saturday, September 15, 2012 5:59 PM
    Saturday, September 15, 2012 5:58 PM
  • Hi Joe,

    What if the HTML files are being stored in 

    Windows.Storage.ApplicationData.Current.LocalFolder

    while the actual application is running? Is it possible to load that HTML file?

    Also, is there a way to write files in the 

    Windows.ApplicationModel.Package.Current.InstalledLocation
    which is in "X:\Program Files\WindowsApps\<app package name>" while the app is running?
    Friday, November 23, 2012 2:11 AM
  • Hi Joe

    Did u find a solution for HTML files stored in

    Windows.Storage.ApplicationData.Current.LocalFolder

    ?

    Monday, December 03, 2012 1:21 PM
  • This worked for me. Thank you very much!
    Monday, January 07, 2013 8:45 PM