Webview2 Winforms



Embed web content (HTML, CSS, and JavaScript) in your native applications with Microsoft Edge WebView2.

Webview2

I am creating a object of Microsoft.Web.WebView2.WinForm.WebView2 but the sub obect of this coreWebView2 is null. Microsoft.Web.WebView2.WinForm.WebView2 webView = new Microsoft.Web.WebView2.WinForm.WebView2 // Change some GUI properties of webView webView.CoreWebView.NavigateUrl(url) // I can not access the above line because CoreWebView is null. #r 'nuget: Diga.WebView2.WinForms, 8.0.1' #r directive can be used in F# Interactive, C# scripting and.NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.

  • Create a new Windows Forms App (.NET). Install the NuGet Microsoft.Web.WebView2 (1.0.705.50). Drag the new WebView2 control onto the form. See the error called out above and in the image below.
  • Microsoft recently announced a preview release of WebView2, a Chromium-based control to host web content within native Windows applications. Developers building applications that support Chromium-bas.
  • Best of the Web

    Use the modern Microsoft Edge (Chromium) platform to bring web experiences into your native app.

  • Power of native

    Build powerful and secure applications by allowing WebView2 controlled access to native capabilities.

  • Evergreen or fixed version

    By default, WebView2 is evergreen and receives automatic updates to stay on the latest and most secure platform.

    A fixed version variant is available for applications with strict compatibility requirements.

Download the WebView2 Runtime

When distributing your application, there are a few ways you can ensure the WebView2 Runtime is on client machines. Learn more about those options.

Evergreen Bootstrapper

The Bootstrapper is a tiny installer that downloads the Evergreen Runtime matching device architecture and installs it locally. There is also a Link that allows you to programmatically download the Bootstrapper.

Evergreen Standalone Installer

A full-blown installer that can install the Evergreen Runtime in offline environment. Available for x86/x64/ARM64.

Fixed Version

Select and package a specific version of the WebView2 Runtime with your application.

-->

Definition

Inheritance
WebView2
Webview2
Implements

Remarks

This control is effectively a wrapper around the WebView2 COM API.You can directly access the underlying ICoreWebView2 interface and all of its functionality by accessing the CoreWebView2 property.Some of the most common COM functionality is also accessible directly through wrapper methods/properties/events on the control.

Upon creation, the control's CoreWebView2 property will be null.This is because creating the CoreWebView2 is an expensive operation which involves things like launching Edge browser processes.There are two ways to cause the CoreWebView2 to be created:

  1. Call the EnsureCoreWebView2Async(CoreWebView2Environment) method. This is referred to as explicit initialization.
  2. Set the Source property. This is referred to as implicit initialization.Either option will start initialization in the background and return back to the caller without waiting for it to finish.To specify options regarding the initialization process, either pass your own CoreWebView2Environment to EnsureCoreWebView2Async or set the control's CreationProperties property prior to initialization.
Example

When initialization has finished (regardless of how it was triggered) then the following things will occur, in this order:

Microsoft Edge Webview2

  1. The control's CoreWebView2InitializationCompleted event will be invoked. If you need to perform one time setup operations on the CoreWebView2 prior to its use then you should do so in a handler for that event.
  2. If a Uri has been set to the Source property then the control will start navigating to it in the background (i.e. these steps will continue without waiting for the navigation to finish).
  3. The Task returned from EnsureCoreWebView2Async(CoreWebView2Environment) will complete.

For more details about any of the methods/properties/events involved in the initialization process, see its specific documentation.

Webview2 Winforms Not Working

Accelerator key presses (e.g. Ctrl+P) that occur within the control willfire standard key press events such as OnKeyDown. You can suppress thecontrol's default implementation of an accelerator key press (e.g.printing, in the case of Ctrl+P) by setting the Handled property of itsEventArgs to true. Also note that the underlying browser process isblocked while these handlers execute, so:

  1. You should avoid doing a lot of work in these handlers.
  2. Some of the WebView2 and CoreWebView2 APIs may throw errors ifinvoked within these handlers due to being unable to communicate withthe browser process.
If you need to do a lot of work and/or invoke WebView2 APIs in response toaccelerator keys then consider kicking off a background task or queuingthe work for later execution on the UI thread.

Constructors

WebView2()

Create a new WebView2 WinForms control.After construction the CoreWebView2 property is null.Call EnsureCoreWebView2Async(CoreWebView2Environment) to initialize the underlying CoreWebView2.

Properties

AllowDrop
CanGoBack

Returns true if the webview can navigate to a previous page in thenavigation history via the GoBack() method.This is equivalent to the CanGoBack.If the underlying CoreWebView2 is not yet initialized, this property is false.

CanGoForward

Returns true if the webview can navigate to a next page in thenavigation history via the GoForward() method.This is equivalent to the CanGoForward.If the underlying CoreWebView2 is not yet initialized, this property is false.

ContextMenu
ContextMenuStrip
CoreWebView2

The underlying CoreWebView2. Use this property to perform more operations on the WebView2 content than is exposedon the WebView2. This value is null until it is initialized. You can force the underlying CoreWebView2 toinitialize via the InitializeAsync method.

CreateParams

Protected CreateParams property. Used to set custom window styles to the forms HWND.

CreationProperties

Gets or sets a bag of options which are used during initialization of the control's CoreWebView2.This property cannot be modified (an exception will be thrown) after initialization of the control's CoreWebView2 has started.

DefaultBackgroundColor

The default background color for the WebView.

Font
IsInDesignMode
Source

The Source property is the URI of the top level document of theWebView2. Setting the Source is equivalent to calling Navigate(String).Setting the Source will trigger initialization of the CoreWebView2, if not already initialized.The default value of Source is null, indicating that the CoreWebView2 is not yet initialized.

Text
ZoomFactor

The zoom factor for the WebView.

Webview2 c# winforms

Methods

What Is Microsoft Edge Webview 2 Runtime

Winforms
Dispose(Boolean)

Cleans up any resources being used.

EnsureCoreWebView2Async(CoreWebView2Environment)

Explicitly trigger initialization of the control's CoreWebView2.

ExecuteScriptAsync(String)

Executes the provided script in the top level document of the WebView2.This is equivalent to ExecuteScriptAsync(String).

GoBack()

Navigates to the previous page in navigation history.This is equivalent to GoBack().If the underlying CoreWebView2 is not yet initialized, this method does nothing.

GoForward()

Navigates to the next page in navigation history.This is equivalent to GoForward().If the underlying CoreWebView2 is not yet initialized, this method does nothing.

NavigateToString(String)

Renders the provided HTML as the top level document of the WebView2.This is equivalent to NavigateToString(String).

OnEnter(EventArgs)

Protected focus handler.

OnGotFocus(EventArgs)

Protected OnGotFocus handler.

OnPaint(PaintEventArgs)

Overrides the base OnPaint event to have custom actionsin designer mode

OnSizeChanged(EventArgs)

Protected SizeChanged handler.

OnVisibleChanged(EventArgs)

Protected VisibilityChanged handler.

ProcessCmdKey(Message, Keys)

Protected command key handler.

Reload()

Reloads the top level document of the WebView2.This is equivalent to Reload().

Select(Boolean, Boolean)

Protected Select method: override this to capture tab direction when WebView control is activated

Stop()

Stops any in progress navigation in the WebView2.This is equivalent to Stop().If the underlying CoreWebView2 is not yet initialized, this method does nothing.

WndProc(Message)

Overrides the base WndProc events to handle specific window messages.

Events

Microsoft.web.webview2.winforms.dll Download

AcceleratorKeyPressed

AcceleratorKeyPressed dispatches when an accelerator key is pressed and before it gets handled by the WebView2.You can write a custom handler to suppress handling by the Webview by doing

.

ContentLoading

ContentLoading dispatches after a navigation begins to a new URI and thecontent of that URI begins to render.This is equivalent to the ContentLoading event.

CoreWebView2InitializationCompleted

This event is triggered either 1) when the control's CoreWebView2 has finished being initialized (regardless of how it was triggered or whether it succeeded) but before it is used for anythingOR 2) the initialization failed.You should handle this event if you need to perform one time setup operations on the CoreWebView2 which you want to affect all of its usages(e.g. adding event handlers, configuring settings, installing document creation scripts, adding host objects).

CoreWebView2Ready

This event is triggered when the control's CoreWebView2 has finished being initialized (regardless of how initialization was triggered) but before it is used for anything.You should handle this event if you need to perform one time setup operations on the CoreWebView2 which you want to affect all of its usages(e.g. adding event handlers, configuring settings, installing document creation scripts, adding host objects).

NavigationCompleted

NavigationCompleted dispatches after a navigate of the top leveldocument completes rendering either successfully or not.This is equivalent to the NavigationCompleted event.

NavigationStarting

NavigationStarting dispatches before a new navigate starts for the toplevel document of the WebView2.This is equivalent to the NavigationStarting event.

SourceChanged

SourceChanged dispatches after the Source property changes. This may happenduring a navigation or if otherwise the script in the page changes theURI of the document.This is equivalent to the SourceChanged event.

WebMessageReceived

WebMessageReceived dispatches after web content sends a message to theapp host via chrome.webview.postMessage.This is equivalent to the WebMessageReceived event.

ZoomFactorChanged

ZoomFactorChanged dispatches when the ZoomFactor property changes.This is equivalent to the ZoomFactorChanged event.

Explicit Interface Implementations

ISupportInitialize.BeginInit()
ISupportInitialize.EndInit()

Webview2 C# Winforms

Applies to





Comments are closed.