Embedding a Web Browser in a Windows 8 Metro C# Application

As you go through the process of building your Metro app, you will probably run into the need to display HTML content.  If you haven’t had the need yet, you are probably wondering, why would you want to display HTML content instead of just embedding the content within a page of your app.  There are several different reasons why, including easy access to your app’s website, help content, account creation, etc.  Regardless of what HTML content you need to display, the XAML WebView provides easy access.

To get started, you will need to place a WebView control within the desired page, within the Grid tag, as shown below.  This markup will display the WebView and use all of the space available.

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <WebView Name="wv"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    </Grid>

Next you will need to tell the WebView what content you want to display.  You can either make use of the Source property in the markup or set the Source in the C# code as shown below.

 wv.Source = new Uri("http://www.google.com/", UriKind.Absolute);

After setting the source property, the WebView will then display the desired page.  In this case, the home page for Google.com.  

If the content you wish to display is available on the web, then you don’t need to do anything else with the WebView.  However, you may need to auto generate the HTML/JavaScript within your app and you will be happy to hear that the WebView supports displaying this content too.  After going through the process of generating the HTML/JavaScript you wish to display, you can display the content by calling the NavigateToString method as shown below:

wv.NavigateToString(“<html>My Content Here</html>”);

While the content is being displayed within the WebView, it will operate just as it would if it originated from the web.  Since you are generating the content to be displayed, you may need to interact with the JavaScript on the page.  You can execute JavaScript method using the InvokeScript method and receive notifications back by using the ScriptNotify event.

Conculsion

Displaying HTML content, while simple, provides a very powerful tool for interacting with users.  The WebView can also be a very powerful tool to reduce the overall development time of your app.  As suggested above, providing easy access to online help materials, is one way in which you can save quite a bit of time in developing your app.  In addition, if you build apps that multi-platform, the WebView may be one way of allowing users to interact with one another such as app forums, etc.



Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds