System.Windows.Controls Nam ...


.NET Framework Class Library
WebBrowser Class

Updated: July 2008

Hosts and navigates between HTML documents. Enables interoperability between WPF managed code and HTML script.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Syntax

Visual Basic (Declaration)
Public NotInheritable Class WebBrowser _
    Inherits ActiveXHost _
    Implements IKeyboardInputSink
Visual Basic (Usage)
Dim instance As WebBrowser
C#
public sealed class WebBrowser : ActiveXHost, 
    IKeyboardInputSink
Visual C++
public ref class WebBrowser sealed : public ActiveXHost, 
    IKeyboardInputSink
JScript
public final class WebBrowser extends ActiveXHost implements IKeyboardInputSink
XAML Object Element Usage
<WebBrowser .../>
Remarks

The WebBrowser control provides the following capabilities:

Navigation: Source, Navigate, NavigateToStream, NavigateToString, Refresh, and Refresh.

Navigation Lifetime: Navigating, Navigated, and LoadCompleted.

Navigation Journaling: CanGoBack, GoBack, CanGoForward, and GoForward.

WPF/HTML Interoperability: InvokeScript and ObjectForScripting, and Document.

WebBrowser is bound by the security constraints of the WPF application that is hosting the WebBrowser:

  • When WebBrowser is hosted by a full-trust WPF application (a stand-alone application, for example), WebBrowser can host HTML documents from any location.

  • When WebBrowser is hosted by a partial-trust WPF application (an XBAP, for example), WebBrowser can only host documents that are Site Of Origin application data files. For more information, see Windows Presentation Foundation Application Resource, Content, and Data Files.

Examples

The following example shows how to configure WebBrowser to navigate to an HTML document by using markup only.

XAML
<!-- Web Browser Control that hosts a web page. -->
<WebBrowser x:Name="webBrowser" Source="http://msdn.com" 
  Width="600" Height="600"  />

The following example shows how to configure WebBrowser to navigate to a document by using markup and code-behind.

XAML
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="addressTextBox" Width="200" />
            <Button Click="goNavigateButton_Click">Go</Button>
        </StackPanel>
        <WebBrowser x:Name="myWebBrowser" />
    </StackPanel>
C#
        private void goNavigateButton_Click(object sender, RoutedEventArgs e)
        {
            // Get URI to navigate to
            Uri uri = new Uri(this.addressTextBox.Text, UriKind.RelativeOrAbsolute);

            // Only absolute URIs can be navigated to
            if (!uri.IsAbsoluteUri)
            {
                MessageBox.Show("The Address URI must be absolute eg 'http://www.microsoft.com'");
                return;
            }

            // Navigate to the desired URL by calling the .Navigate method
            this.myWebBrowser.Navigate(uri);
        }

Inheritance Hierarchy

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows.Media..::.Visual
        System.Windows..::.UIElement
          System.Windows..::.FrameworkElement
            System.Windows.Interop..::.HwndHost
              System.Windows.Interop..::.ActiveXHost
                System.Windows.Controls..::.WebBrowser
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5 SP1, 3.0 SP2
See Also

Reference

Other Resources

Change History

Date

History

Reason

July 2008

Added topic for new class.

SP1 feature change.

Tags :


Community Content

Thomas Lee
Problem
The control will not be shown if the Window where the control is in has AllowsTransparency="True", any solutions?
Tags :

Fixme
NewWindow event?

The old Windows Forms WebBrowser control had a NewWindow event which made it possible to intercept the opening of Internet Explorer windows (right-click -> open in new window or links with target="_blank").

Is this possible to do with the System.Control.WebBrowser? Otherwise, such navigation is completely beyond my control :-(

Tags :

antonpious
Location of this class in the assembly

Is there a PresentationFramework.dll for .net 3.5? At present I could see only for 3.0 and this control is not present under System.Windows.Controls

You need to install .net 3.5 SP1. Due to version issues this control is still added to the PresentationFramework.dll and version still stays the same 3.0 even after the upgrade to 3.5 SP1

Tags :

Chango V. - MSFT
Getting to the native IWebBrowser2

[re-posted after a wiki malfunction]

There is much functionality of the native Web Browser control that our managed wrapper does not yet expose. The following code snippet [prepared with Matt G.] shows how to get the IWebBrowser2 interface from the WPF WebBrowser control. This allows access to methods on the object that are not publicly exposed in other ways for the control. Do note, however, that this code sample will only work in fully trusted code.

First, see IWebBrowser2 documentation here: http://msdn.microsoft.com/en-us/library/aa752127.aspx ...

To compile this code, add a COM Reference to System32\shdocvw.dll or ieframe.dll (whichever you have, depending on version of IE).

[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]

[Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]

internal interface IServiceProvider

{

[return: MarshalAs(UnmanagedType.IUnknown)]

object QueryService(ref Guid guidService, ref Guid riid);

}

static readonly Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");

...

IServiceProvider serviceProvider = (IServiceProvider)myWebBrowser.Document;

Guid serviceGuid = SID_SWebBrowserApp;

Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;

SHDocVw.IWebBrowser2 myWebBrowser2 = (SHDocVw.IWebBrowser2) serviceProvider.QueryService(ref serviceGuid, ref iid);

...

And then myWebBrowser2 is ready for interaction.

You can also handle the native web browser's events (http://msdn.microsoft.com/en-us/library/aa768309(VS.85).aspx) through the generated managed wrappers, like this:

SHDocVw.DWebBrowserEvents_Event wbEvents = (SHDocVw.DWebBrowserEvents_Event)myWebBrowser2;
wbEvents.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(OnWebBrowserNewWindow);

void OnWebBrowserNewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
{
// Set Processed to cancel opening of the new window.
}

Tags : iwebbrowser2

Chango V. - MSFT
WebBrowser and Window.AllowsTransparency
http://blogs.msdn.com/changov/archive/2009/01/19/webbrowser-control-on-transparent-wpf-window.aspx.
Tags :

Chango V. - MSFT
Getting ActiveXInstance
Chango's update was extremely helpful; about 90% of the functionality not exposed through the WPF control is available through the IWebBrowser2 interface. Is there any way to get at the equivalent of the old web browser control's "ActiveXInstance" property, which allows access to the underlying IOleObject, which is the other 10% of that functionality. I'm trying to do things like implement IDocHostShowUI and IDocHostUIHandler, to customize the browser a little.

Thanks!

-Dan

[Chango] You should be able to just cast IWebBrowser2 to the other OLE interfaces. It's the same object.
Tags :

Page view tracker