Introduction
In your projet, you probably need to generate dynamicaly html, or to get html fragment from a web service and to display it in the WebBrowser control. In those cases, you would not load at any times the js, css or images from the network but you could include it in your application package (xap).
Solution
To demonstrate how to do that, we will take an exemple that will get html code from a web service:
<div data-role='fieldcontain'>
<fieldset data-role='controlgroup'>
<legend>Do you agree to this request?</legend>
<input type='checkbox' name='agree-1' id='agree-1' />
<label for='agree-1'>I agree</label>
</fieldset>
</div>
As you see, this html calls data-role from jquery Mobile. In our demo, we will include jqueryMobile js and css files in our xap package.

The fist step is to add your files in a directory, for exemple: html and to set the "Build Action" to "Content".
The next step, is to concat the html received from the WebService with the our html body. For that, we will write a method BuildHTML.
public string BuildHTML(string htmlFromWS)
{
StringBuilder html = new StringBuilder(@"<!DOCTYPE html>
<html class=""ui-mobile-rendering"">
<head>
<meta charset=""utf-8"" />
<meta name=""viewport"" content=""initial-scale=1.0; maximum-scale=1.0"" />
<link rel=""stylesheet"" type=""text/css"" href=""/html/jquery.mobile.structure-1.0.1.min.css"" />
<link rel=""stylesheet"" type=""text/css"" href=""/html/style.css"" />
<script type=""text/javascript"" src=""/html/jquery-1.7.1.min.js""></script>
<script type=""text/javascript"" src=""/html/jquery.mobile-1.0.1.min.js""></script>
</head>
<body style=""-webkit-user-select: none;"">
<div style=""padding:80px 0 0 0"" data-role=""page"">
<div data-role=""content"">");
html.Append(htmlFromWS);
html.Append("</div></div></body></html>");
return html.ToString();
}
As you see, the css and the javascript is referenced with the url /html/... this means that the webbrowser will load the files from the isolatedstorage, with a relative Uri. Unfortunately, adding the files as Content in the solution, does'nt add them into the isolatedstorage. For that, we will write the method CopyContentToIsolatedStorage.
public static void CopyContentToIsolatedStorage(string file)
{
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
if (iso.FileExists(file))
return;
var fullDirectory = System.IO.Path.GetDirectoryName(file);
if (!iso.DirectoryExists(fullDirectory))
iso.CreateDirectory(fullDirectory);
using (Stream input = Application.GetResourceStream(new Uri(file, UriKind.Relative)).Stream)
{
using (IsolatedStorageFileStream output = iso.CreateFile(file))
{
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
output.Write(readBuffer, 0, bytesRead);
}
}
}
}
and now, you can add these code in the beginning of you page:
CopyContentToIsolatedStorage("html/themes/images/ajax-loader.png");
CopyContentToIsolatedStorage("html/themes/images/icons-18-black.png");
CopyContentToIsolatedStorage("html/themes/images/icons-18-white.png");
CopyContentToIsolatedStorage("html/themes/images/icons-36-black.png");
CopyContentToIsolatedStorage("html/themes/images/icons-36-white.png");
CopyContentToIsolatedStorage("html/jquery-1.7.1.min.js");
CopyContentToIsolatedStorage("html/jquery.mobile-1.0.1.min.js");
CopyContentToIsolatedStorage("html/style.css");
CopyContentToIsolatedStorage("html/jquery.mobile.structure-1.0.1.min.css");
and then, we have just to concat the html and to send it to the WebBrowser control. But, be carefull, there is a tip, to force the WebBrowser to load the html and it's files ! You have to save an html file into the isolatedstorage in the root folder of your css/js files.
string htmlFromWS = ...;
string html = BuildHTML(htmlFromWS);
var bytes = Encoding.UTF8.GetBytes(html);
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream output = iso.CreateFile("html/index.html"))
{
output.Write(bytes, 0, bytes.Length);
}
wb.Navigate(new Uri("html/index.html", UriKind.Relative));
And that's it, the WebBrowser control will load the css, js and your html from the isolatedstorage ! You can find the source code here : Download Source_code.zip - 102.33 KB