Skip to main content
September 17, 2013
Windows Phone Developer Blog

Windows Phone WebBrowser control tips

This blog post was authored by Chee Chen Tong, a Program Manager on the Windows Phone team.

– Adam

Including a WebBrowser control in your Windows Phone app is a strategic way to integrate web content in your app. It can help you extend the value of your investments, because you don’t have to recreate content to use it on different platforms. Many of the more than 30% of Windows Phone apps that incorporate the WebBrowser control in their design integrate the control and the web contents within the app seamlessly simply using XAML properties. Frameworks such as PhoneGap use the WebBrowser control to display HTML content, and they use CSS and JavaScript to further expand cross-platform capabilities. Below are some common scenarios and tips that may be helpful when using the WebBrowser control in your Windows Phone app.

Setting the WebBrowser background color

You might want the background of the web content in your app to match your app’s overall design, or even match the theme the app user has set for their phone. Unfortunately, no matter what color you set for the WebBrowser control background, the user will see a flash of white background while the control initializes, and before the web content is loaded. This is because the WebBrowser control loads a white about:blank page during initialization, before any navigation occurs. Although Windows Phone 8 supports the DefaultBackground color property, the effective way to use this property depends on a certain sequence of events. You need to set the visibility of the WebBrowser control after the content has been navigated to or after the content has loaded. This code example illustrates how to do this:

  1. private void NavigateHandler(object sender, NavigationEventArgs e)
  2. {
  3.     WebBrowser wb = sender as WebBrowser;
  4.     if (wb != null)
  5.     {
  6.         wb.Visibility = Visibility.Visible;
  7.     }
  8. }
  10. private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
  11. {
  12.     WebBrowser webBrowser = new WebBrowser();
  13.     webBrowser.Background = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
  14.     webBrowser.Navigate(new Uri(“”, UriKind.Absolute));
  16.     webBrowser.Navigated += new EventHandler<NavigationEventArgs>(NavigateHandler);
  18.     webBrowser.Visibility = Visibility.Collapsed;
  19.     webBrowser.Height = 543;
  20.     webBrowser.Width = 456;
  21.     webBrowser.IsScriptEnabled = true;
  23.     ContentPanel.Children.Add(webBrowser);
  24. }

While content is loading, and because the WebBrowser control itself is not yet visible, we recommend that you provide a visual progress indicator for the app user, either using the ProgressBar control, or with a spinner UI, for example.

Rendering WebBrowser control content onto a bitmap

Sharing is becoming a normal activity on the phone, and many app developers prefer to capture screenshots to use in sharing scenarios. On a typical page, you can use WriteableBitmap and pass it the LayoutRoot of a page to capture a screenshot of the page. In earlier releases of Windows Phone, if a WebBrowser control was embedded in the page, a blank rectangle displayed where the WebBrowser control was on the page. In Windows Phone 8, though, you can capture the content of a WebBrowser control with a few lines of code:

  1. WriteableBitmap bitmap = new WriteableBitmap(400, 400);
  2. bitmap.Render(Browser, null);
  3. bitmap.Invalidate();
  4. ImageElement.Source = bitmap;

WebBrowser control Hold event not being raised

One of the things an app developer commonly would want to do is handle certain gesture events that aren’t supported by the WebBrowser control itself. The WebBrowser control doesn’t bubble up events, so you don’t have a reliable way to execute custom logic in response to specific user input.

One of these types of events is the Hold event. The WebBrowser control doesn’t provide a context menu for the Hold event, and the event is being consumed by the container control that hosts the WebBrowser control. If you followed the visual tree to get the container control, registering for the Hold event will work. The following code example shows you how to handle the Hold event, and to provide a context menu if you need to:

  1. private void webBrowser _Loaded(object sender, RoutedEventArgs e)
  2. {
  3.     var border0 = VisualTreeHelper.GetChild(sender, 0);
  4.     var border1 = VisualTreeHelper.GetChild(border0, 0);
  5.     var Container = VisualTreeHelper.GetChild(border1, 0);
  7.     if (Container!= null)
  8.     {
  9.         Container.Hold += Border_Hold;
  10.     }
  11. }
  13. void Border_Hold(object sender, GestureEventArgs e)
  14. {
  15.     e.Handled = true;
  16. }

Be sure to see Mark Hopkins’s blog post about hybrid web apps titled Make your mobile website into a Windows Phone app. For more information about Windows Phone web development, see Web development for Windows Phone.