How to display web content from the network using the WebBrowser control for Windows Phone 8

Applies to: Windows Phone 8 and Windows Phone Silverlight 8.1 | Windows Phone OS 7.1

You can host a WebBrowser control in an application to display web content from the network. For example, a developer may want to display recipes from the network inside of a recipe application.

You can accomplish this by hosting a WebBrowser control inside the application, and changing the location of the control using its Source property or the Navigate(Uri) method.

Caution noteCaution:

Script is disabled in the WebBrowser control by default. Set the IsScriptEnabled property to true if you want to enable scripting in your control.

The following code example shows how you can update the Source property of the WebBrowser control from inside of a .xaml file:

<phone:WebBrowser Source="" />

Alternatively, if the WebBrowser control has a name specified in the .xaml file, you can update the Source property from within the code-behind file. The following code sample shows how you can update the Source property of a WebBrowser control that has been named webBrowser1 in the .xaml file:

webBrowser1.Source = new Uri("", UriKind.Absolute);

Alternatively, you can use the Navigate(Uri) method of the WebBrowser class to accomplish the same goal:

webBrowser1.Navigate(new Uri("", UriKind.Absolute));

If you choose to call a method rather than set a property, keep in mind that an InvalidOperationException is thrown if the WebBrowser control is not yet in the visual tree. To avoid this, you can attach a handler to the Loaded event to ensure that the control is in the visual tree before the method is called:

webBrowser1.Loaded += (object sender, RoutedEventArgs e) =>
    webBrowser1.Navigate(new Uri("", UriKind.Absolute));

© 2015 Microsoft