Partager via


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

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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.

Warning

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="https://www.bing.com" />

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("https://www.bing.com", UriKind.Absolute);

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

webBrowser1.Navigate(new Uri("https://www.bing.com", 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("https://www.bing.com", UriKind.Absolute));
};

See Also

Reference

WebBrowser

Other Resources

WebBrowser control for Windows Phone 8