A Better Browsing Experience: Listing 3.

Using InvokeScript to load CSS content.

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.

comments powered by Disqus

Featured

  • How to Do Machine Learning Evolutionary Optimization Using C#

    Resident data scientist Dr. James McCaffrey of Microsoft Research turns his attention to evolutionary optimization, using a full code download, screenshots and graphics to explain this machine learning technique used to train many types of models by modeling the biological processes of natural selection, evolution, and mutation.

  • Old Stone Wall Graphic

    Visual Studio Code Boosts Java Dependency Viewer

    Easier management of project code dependencies and improvements to extensions for popular Java frameworks and runtimes highlight the February update to Java in Visual Studio Code functionality.

  • Blule Squares

    Visual Studio 2019 for Mac 8.5 Preview Adds ASP.NET Core Authentication

    Microsoft, after shipping Visual Studio 2019 for Mac v8.4 with support for ASP.NET Core Blazor Server applications last month, is now previewing the v8.5 series, adding new authentication templates for ASP.NET Core along with other improvements.

  • Q&A with Brice Wilson: What's New in Angular 9

    We caught up with expert web developer/trainer Brice Wilson to get his take on Angular, which always appears at or near the top of periodic rankings of the most popular JavaScript-based web development frameworks.

  • Entity Framework Core Migrations

    Eric Vogel uses code samples and screenshots to demonstrate how to use Entity Framework Core migrations in a .NET Core application through the command line and in code.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events