How to display dynamically generated web content using the WebBrowser control for Windows Phone 8

How to display dynamically generated web content 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. ]

This topic describes how you can use the NavigateToString(String) method of the WebBrowser control to display dynamically generated web content inside of the control, using XAML and C#. You should review WebBrowser control security considerations before using this method. For more information, see WebBrowser control security best practices for Windows Phone 8.

This topic contains the following sections.

You can add a WebBrowser control either using the tools or manually.

Adding a WebBrowser Control Using the Tools

  1. Open a new or existing solution in Visual Studio.

  2. Click Toolbox while viewing the XAML file for the project, and drag the WebBrowser control into the device image.

Adding a WebBrowser Control Manually

You can add a WebBrowser control manually by creating the control in XAML.

To create a WebBrowser control in XAML

  1. Open the XAML file for the page in which you will add the WebBrowser control. In Solution Explorer, right-click the .xaml file for the page (by default, the main page for a new application is called "MainPage.xaml") and select Open.

  2. Add a WebBrowser control inside of ContentGrid. For example:

    
    <Grid x:Name="ContentGrid" Grid.Row="1">
        <phone:WebBrowser HorizontalAlignment="Left" Margin="20,50,0,0" Name="webBrowser1" VerticalAlignment="Top" Height="500" Width="430" />
    </Grid>
    
    

Update the code-behind page to include the call to NavigateToString. For example, if you are using the main page with the default naming convention, you would update MainPage.xaml.cs. The following sample code describes one way to do this:

public MainPage()
{
      InitializeComponent();
      SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

      webBrowser1.Loaded += WebBrowser_OnLoaded;
}

private void WebBrowser_OnLoaded(object sender, RoutedEventArgs e)
{
      webBrowser1.NavigateToString("<html><head><meta name='viewport' content='width=480, user-scalable=yes' /></head><body>HTML Text</body></html>"); 
}

Show:
© 2016 Microsoft