Table of Contents

Class WebBrowser

Namespace
Alternet.UI
Assembly
Alternet.UI.dll

This control may be used to render full featured web documents. It supports using multiple backends, corresponding to different implementations of the same functionality.

[ControlCategory("Common")]
public class WebBrowser : Control, IBaseObject, IDisposableObject, IInputElement, ISupportInitialize, IFocusable, IWin32Window, ITextProperty, IComponent, IDisposable, IWebBrowser, IWebBrowserLite
Inheritance
WebBrowser
Implements
Extension Methods

Examples

Here is how to declare a WebBrowser in UIXML:

<WebBrowser
      Name="WebBrowser1"
      VerticalAlignment="Stretch"
      Navigated="WebBrowser1_Navigated"
      Loaded="WebBrowser1_Loaded"
      NewWindow="WebBrowser1_NewWindow"
      DocumentTitleChanged="WebBrowser1_TitleChanged"
        />

And the corresponding event handler in the code-behind:

private void WebBrowser1_Navigated(object sender, WebBrowserEventArgs e)
{
    UrlTextBox.Text = WebBrowser1.GetCurrentURL();
}

private void WebBrowser1_Loaded(object sender, WebBrowserEventArgs e)
{
    UpdateHistoryButtons();
    if (!pandaLoaded)
    {
        pandaLoaded = true;
        WebBrowser1.PreferredColorScheme = WebBrowserPreferredColorScheme.Light;

        WebBrowser1.LoadURL("https://www.google.com/");
    }
}

private void WebBrowser1_NewWindow(object sender, WebBrowserEventArgs e)
{
    WebBrowser1.LoadURL(e.Url);
}

private void WebBrowser1_TitleChanged(object sender, WebBrowserEventArgs e)
{
    HeaderLabel.Text = "Web Browser Control: " + e.Text;
}

Also, a WebBrowser can be created from code:

var wb = new Alternet.UI.WebBrowser();
wb.LoadURL("https://www.google.com");

Remarks

Example of how a WebBrowser can look:

WebBrowser

Constructors

WebBrowser()

Initializes a new instance of the WebBrowser class.

WebBrowser(string)

Initializes a new instance of the WebBrowser class.

Properties

AccessToDevToolsEnabled

Enables or disables access to developer tools for the user.

Backend

Gets the current WebBrowser backend.

CanCopy

Gets a value indicating whether the current selection can be copied, which allows the Copy() method to succeed.

CanCut

Gets a value indicating whether the current selection can be cut, which allows the Cut() method to succeed.

CanGoBack

Gets a value indicating whether a previous page in navigation history is available, which allows theGoBack() method to succeed.

CanGoForward

Gets a value indicating whether a subsequent page in navigation history is available, which allows the GoForward() method to succeed.

CanPaste

Gets a value indicating whether the current selection can be replaced with the contents of the Clipboard, which allows the Paste() method to succeed.

CanRedo

Gets a value indicating whether the user can redo the previous operation in the control.

CanUndo

Gets a value indicating whether the user can undo the previous operation in the control.

CanZoomIn

Gets a value indicating whether the zoom factor of the page can be increased, which allows the ZoomIn() method to succeed.

CanZoomOut

Gets a value indicating whether the zoom factor of the page can be decreased, which allows the ZoomOut() method to succeed.

ContextMenuEnabled

Enables or disables the right click context menu.

ControlKind

Returns control identifier.

DefaultUrl

Gets default url (the first loaded url).

Editable

Gets or sets whether the control is currently editable.

HasBorder

Gets or sets a value indicating whether the control has a border.

HasSelection

Gets a value indicating whether there is a current selection in the control.

Is64Bit

Gets a value indicating whether the WebBrowser runs on a 64 bit platform.

IsBusy

Gets a value indicating whether the control is currently busy (e.g. loading a web page).

MemoryFS

Contains methods related to the memory scheme WebBrowser protocol.

PageSource

Gets the HTML source code of the currently displayed document or an empty string if no page is currently shown.

PageText

Gets the text of the current page.

PreferredColorScheme

Sets the overall color scheme of the WebBrowser.

SelectedSource

Gets the HTML source code of the currently selected portion of the web page or an empty string if no selection exists.

SelectedText

Gets a value indicating the currently selected text in the control.

Source

Gets or sets the Uri of the current document hosted in the WebBrowser.

Url

Gets or sets the URL of the current document hosted in the WebBrowser.

UserAgent

Gets or sets the custom user agent string for the WebBrowser control.

Zoom

Gets or sets the zoom level of the page.

ZoomFactor

Gets or sets the zoom factor of the page.

ZoomType

Gets or sets how the zoom factor is currently interpreted by the HTML engine.

Methods

AddScriptMessageHandler(string)

Adds a script message handler with the given name.

AddUserScript(string, bool)

Injects the specified script into the webpage's content.

CanSetZoomType(WebBrowserZoomType)

Retrieve whether the current HTML engine supports a zoom type.

ClearHistory()

Clear the history, this will also remove the visible page.

ClearSelection()

Clears the current selection.

Copy()

Copies the current selection in the control to the Clipboard.

CrtSetDbgFlag(int)

Retrieves or modifies the state of the debug flag to control the allocation behavior of the debug heap manager.

Cut()

Moves the current selection in the control to the Clipboard.

DeleteSelection()

Deletes the current selection.

DoCommand(string, params object?[])

Executes a browser command with the specified name and parameters.

DoCommandGlobal(string, params object?[])

Executes the browser command with the specified name and parameters. This is the static version of DoCommand(string, params object?[]).

EnableHistory(bool)

Enables or disables the history.

Find(string, WebBrowserFindParams?)

Finds a text on the current page and if found, the control will scroll the text into view and select it.

FindClearResult()

Resets the search and the highlighted results.

GetBackendOS()

Returns type of the OS for which the WebBrowser was compiled.

GetBackendVersionString(WebBrowserBackend)

Retrieve the version information about the browser backend implementation.

GetCurrentTitle()

Get the title of the current web page.

GetCurrentURL()

Get the URL of the currently displayed document.

GetLibraryVersionString()

Retrieve the version information about the underlying library implementation.

GetNativeBackend()

Returns pointer to the native backend interface.

GoBack()

Navigates the control to the previous page in the navigation history, if one is available.

GoForward()

Navigates the control to the subsequent page in the navigation history, if one is available.

InvokeScriptAsync(string, nint, params object?[])

Executes the given JavaScript function asynchronously and returns the result via a ScriptResult event.

IsBackendAvailable(WebBrowserBackend)

Allows to check if a specific WebBrowser backend is currently available.

LoadURL(string?)

Loads a web page from a URL.

LoadUrlOrSearch(string)

Loads a web page from a URL if it is in a valid format. Otherwise opens passed URL in the Google search.

Navigate(string)

Loads the document at the specified Uniform Resource Locator (URL) into the WebBrowser control, replacing the previous document.

Navigate(Uri)

Loads the document at the location indicated by the specified Uri into the WebBrowser control, replacing the previous document.

NavigateToStream(Stream)

Navigate to a Stream that contains the content for a document.

NavigateToString(string, string?)

Sets the displayed page source to the contents of the given string.

ObjectToJSON(object?)

Converts the value into a JSON string. It is a simple convertor. It is better to use System.Text.Json.JsonSerializer.Serialize method.

OnBeforeBrowserCreate(WebBrowserEventArgs)

Raises the BeforeBrowserCreate event.

OnDocumentTitleChanged(WebBrowserEventArgs)

Raises the DocumentTitleChanged event.

OnError(WebBrowserEventArgs)

Raises the Error event.

OnFullScreenChanged(WebBrowserEventArgs)

Raises the FullScreenChanged event.

OnLoaded(WebBrowserEventArgs)

Raises the Loaded event.

OnNavigated(WebBrowserEventArgs)

Raises the Navigated event.

OnNavigating(WebBrowserEventArgs)

Raises the Navigating event.

OnNewWindow(WebBrowserEventArgs)

Raises the NewWindow event.

OnScriptMessageReceived(WebBrowserEventArgs)

Raises the ScriptMessageReceived event.

OnScriptResult(WebBrowserEventArgs)

Raises the ScriptResult event.

Paste()

Replaces the current selection in the control with the contents of the Clipboard.

PrepareFileUrl(string)

Prepends filename with "file" url protocol prefix.

Print()

Opens a print dialog so that the user may change the current print and page settings and print the currently displayed page.

Redo()

Redos the last edit operation in the control.

Reload()

Reloads the document currently displayed in the control by downloading for an updated version from the server.

Reload(bool)

Reloads the document currently displayed in the control using the specified refresh option.

RemoveAllUserScripts()

Removes all user scripts from the WebBrowser.

RemoveScriptMessageHandler(string)

Remove a script message handler with the given name that was previously added using AddScriptMessageHandler(string).

RunScriptAsync(string, nint?)

Runs the given JavaScript code asynchronously and returns the result via a ScriptResult event.

SelectAll()

Selects the entire page.

SetBackend(WebBrowserBackend)

Sets the backend that will be used for the new WebBrowser instances.

SetBackendPath(string, bool)

Sets path to a fixed version of the WebView2 Edge runtime.

SetDefaultFSNameArchive(string)

Sets the default protocol name for the archive file system that will be used for the new WebBrowser instances.

SetDefaultFSNameMemory(string)

Sets the default protocol name for the memory file system that will be used for the new WebBrowser instances.

SetDefaultScriptMesageName(string)

Sets the default script message name that will be registered in all new WebBrowser instances.

SetDefaultUserAgent(string)

Sets the default user agent that will be used for the new WebBrowser instances.

SetLatestBackend()

Sets the best possible backend to be used for the new WebBrowser instances.

SetVirtualHostNameToFolderMapping(string, string, WebBrowserHostResourceAccessKind)

Sets a mapping between a virtual host name and a folder path to make available to web sites via that host name.

Stop()

Stops the current page loading process, if any. Cancels any pending navigation and stops any dynamic page elements, such as background sounds and animations.

ToInvokeScriptArg(object?)

Converts the value to the JSON string.

ToInvokeScriptArgs(object?[])

Converts the array of object values to the comma delimited JSON string.

Undo()

Undoes the last edit operation in the control.

ZoomIn()

Increases the zoom factor of the page.

ZoomOut()

Decreases the zoom factor of the page.

Events

BeforeBrowserCreate

Occurs after backend is created, but before actual browser control creation.

DocumentTitleChanged

Occurs when the web page title changes. Use Text to get the title.

Error

Occurs when a navigation error occurs.

FullScreenChanged

Occurs when the the page wants to enter or leave fullscreen.

Loaded

Occurs when the WebBrowser control finishes loading a document.

Navigated

Occurs when the WebBrowser control has navigated to a new web page and has begun loading it.

Navigating

Occurs before the WebBrowser control navigates to a new web page.

NewWindow

Occurs when a new browser window is created.

ScriptMessageReceived

Occurs when your application receives message from JS code of the loaded web page.

ScriptResult

Occurs when your application receives results after RunScriptAsync call.