I'm creating an app on Windows Phone 7 that lets folks read offline HTML content. Is there a way to show an inline browser or HTML viewing control?
views:
209answers:
2Yes, there's a WebBrowser
control in Microsoft.Phone.Controls
If you then save your (offline) files to IsolatedStorage
you can then view the file via a call like this from code:
webBrowser1.Navigate(new Uri("offline-file-name.html", UriKind.Relative));
Things to note:
You can use directories within isolated storage. Just specify the whole path in the Uri.
If navigating between offline pages, all paths must be relative.
The WebBrowser control allows you to display arbitrary HTML.
There are two methods on the WebBrowser control you can use to display HTML:
- Navigate() will display the contents of a file located at the supplied URL. This file can be in IsolatedStorage or on a server.
- NavigateToString() will display the html string you give it.
In your case I'm guessing you'd use a WebClient object to download the webpage to offline storage, then the Navigate method to display it.
However, the benefit of NavigateToString is that you can munge the string on the way in to do some cool (or essential) stuff like style the page so it matches perfectly with the current phone theme, or catch all link clicks and make them pop in the external browser (otherwise they load in the very same WebBrowser control).
I've documented the details of styling and link clickery here: http://www.ben.geek.nz/2010/07/integrated-links-and-styling-for-windows-phone-7-webbrowser-control/