Duplicate Transaction
WebView in WinForms

WebView for Winforms and WPF

If you need to load a webpage in your C# WinForm app, you have to use a WebBrowser control, which comes with Visual Studio.
WebBrowser is, in fact, a wrapper for IE, and it’s stuck in IE.
You will notice that when your app loads a webpage and page does not look like what you expected! Another case is when you get a message that wants you to update your web browser (IE).

Look at my article in Code Project: Browser Update for WebBrowser control in VB.NET

The good news is that you now can use WebView, one of several wrapped Universal Windows Platform controls that are available for Windows Forms and WPF applications, to your app.
This control uses the Microsoft Edge rendering engine (EdgeHTML) to embed a view that renders richly formatted HTML5 content from a remote web server, dynamically generated code, or content files.

Feature limitations

When compared to the UWP WebView control, the current release of the WPF and Windows Forms WebView control has some limitations. For the complete list of these limitations, see Known Issues of the WebView control for Windows Forms and WPF applications.

See also the FAQs section below for answers to common questions when WebView for Windows Forms and WPF applications.

Before we go any further you need to know the prerequisites:

  • Visual Studio, 2017.
  • Windows 10 Insider Preview Build 17110 or a later release.
  • .NET Framework 4.6.2 or a later release

Add the WebView control 

  1. Assuming that your application meets these requirements, let’s proceed. Install Microsoft.Toolkit.Forms.UI.Controls.WebView NuGet package.
  2. Open the Toolbox in Visual Studio. The WebView control appears in the General section of the Toolbox, and you can drag it directly to the designer.
private void WebView_Loaded(object sender, RoutedEventArgs e)
{
webView1.Navigate("http://www.duplicatetransaction.com");
}

The WebView control provides several events that you can use to respond to navigation and content loading states. The events occur in the following order for the root web view content:

NavigationStarting

webView1.NavigationStarting += webView1_NavigationStarting;

private void webView1_NavigationStarting(object sender, WebViewControlNavigationStartingEventArgs args)
{
    // Cancel navigation if URL is not allowed. (Implemetation of IsAllowedUri not shown.)
    if (!IsAllowedUri(args.Uri))
        args.Cancel = true;
}

ContentLoading

webView1.ContentLoading += webView1_ContentLoading;

private void webView1_ContentLoading(WebView sender, WebViewControlContentLoadingEventArgs args)
{
    // Show status.
    if (args.Uri != null)
    {
        statusTextBlock.Text = "Loading content for " + args.Uri.ToString();
    }
}

DOMContentLoaded

webView1.DOMContentLoaded += webView1_DOMContentLoaded;

private void webView1_DOMContentLoaded(WebView sender, WebViewControlDOMContentLoadedEventArgs args)
{
    // Show status.
    if (args.Uri != null)
    {
        statusTextBlock.Text = "Content for " + args.Uri.ToString() + " has finished loading";
    }
}

NavigationCompleted

webView1.NavigationCompleted += webView1_NavigationCompleted;

private void webView1_NavigationCompleted(WebView sender, WebViewControlNavigationCompletedEventArgs args)
{
    if (args.IsSuccess == true)
    {
        statusTextBlock.Text = "Navigation to " + args.Uri.ToString() + " completed successfully.";
    }
    else
    {
        statusTextBlock.Text = "Navigation to: " + args.Uri.ToString() +
                               " failed with error " + args.WebErrorStatus.ToString();
    }
}

Known and potential issues

There are several potential and known issues in using WebView in WinForms applications.
What I like to point out is after dragging WebView Control to a WinForms Application.
A Form with a WebView control cannot run and code and freezes at the moment WebView control added.
As the form controls begin to add, the whole application stops, and you cannot see the mainform. In other words, the Load_Form() event never starts!

It took me a good 24 hours of search and research to finally figure out what is the root cause of this issue!

The solution may look silly, but if you have launched VS as Administrator, WebView control acts buggy, and your WinForm application cannot add controls or trigger any events! Exit VS and run it without Administrator and voila!

Reference: https://docs.microsoft.com/en-us/windows/communitytoolkit/controls/wpf-winforms/webview

Kourosh

Your Header Sidebar area is currently empty. Hurry up and add some widgets.