Click here to Skip to main content
Licence CPOL
First Posted 20 Feb 2012
Views 3,815
Bookmarked 2 times

Include static JS / CSS / Images files from the IsolatedStorage in the WebBrowser control

By | 20 Feb 2012 | Article
Include static JS / CSS / Images files from the IsolatedStorage in the WebBrowser control

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)
{
    // Obtain the virtual store for the application.
    IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
 
    if (iso.FileExists(file))
        return;
 
    var fullDirectory = System.IO.Path.GetDirectoryName(file);
    if (!iso.DirectoryExists(fullDirectory))
        iso.CreateDirectory(fullDirectory);
 
    // Create a stream for the file in the installation folder.
    using (Stream input = Application.GetResourceStream(new Uri(file, UriKind.Relative)).Stream)
    {
        // Create a stream for the new file in isolated storage.
        using (IsolatedStorageFileStream output = iso.CreateFile(file))
        {
            // Initialize the buffer.
            byte[] readBuffer = new byte[4096];
            int bytesRead = -1;
 
            // Copy the file from the installation folder to isolated storage. 
            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 = ...;
// Concat our html
string html = BuildHTML(htmlFromWS);
var bytes = Encoding.UTF8.GetBytes(html);
// Get the iso store
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
// Write the html in the html/index.html
using (IsolatedStorageFileStream output = iso.CreateFile("html/index.html"))
{
    output.Write(bytes, 0, bytes.Length);
}
// navigate to the html/index.html
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Nicolas Humann

Technical Lead

France France

Member

Follow on Twitter Follow on Twitter
Specialized in .net technologies for many years, I am a technology fan in both asp.net and wpf/silverlight, using c# and .NET 4.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 20 Feb 2012
Article Copyright 2012 by Nicolas Humann
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid