A Better Browsing Experience: Listing 3.
Using InvokeScript to load CSS content.
- By Nick Randolph
- 08/01/2012
private void BrowserLoadCompleted(object sender, NavigationEventArgs e)
{
ApplyStyleSheet("mobile.css");
VisualStateManager.GoToState(this, "Loaded", true);
}
private void ApplyStyleSheet(string cssFilename)
{
var sr = Application.GetResourceStream(new Uri(cssFilename, UriKind.Relative));
using (var strm = sr.Stream)
using (var reader = new StreamReader(strm))
{
string css = reader.ReadToEnd().Replace("\r", "").Replace("\n", "");
var scriptToRun =
"(function() {" +
" var pa= document.getElementsByTagName('head')[0] ; " +
" var el= document.createElement('style'); " +
" el.type= 'text/css'; " +
" el.media= 'screen'; " +
" el.styleSheet.cssText= '" + css + "'; " +
" pa.appendChild(el); " +
"})();";
Browser.InvokeScript("eval", scriptToRun);
}
}
About the Author
Nick Randolph runs Built to Roam, a consulting company that specializes in training, mentoring and assisting other companies build mobile applications. With a heritage in rich client applications for both the desktop and a variety of mobile platforms, Nick currently presents, writes and educates on the Windows Phone platform.