7

I am trying to add a WebView to a WinForm in order to use a modern browser in an application.

Starting with a blank WinForm, I added code to create the WebView and add it to the form's controls.

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestWebView
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

            var wvc = new Microsoft.Toolkit.Win32.UI.Controls.WinForms.WebView();
            ((ISupportInitialize)wvc).BeginInit();
            wvc.Dock = DockStyle.Fill;
            Controls.Add(wvc);
            ((ISupportInitialize)wvc).EndInit();

            // You can also use the Source property
            wvc.Navigate(new Uri("https://www.microsoft.com"));
        }
    }
}

This compiles and runs, but the EndInit() call does not finish. No exceptions are thrown. The call enters but does not leave.

The project is set for .NET Framework 4.7.1. I used the NuGet Manager to add Microsoft.Toolkit.WIn32.UI.Controls v4.0.2 to the project. I am following the instructions on https://blogs.windows.com/msedgedev/2018/05/09/modern-webview-winforms-wpf-apps/

Why is this not working?

Robert Deml
  • 12,390
  • 20
  • 65
  • 92
  • if you move the Controls.Add out of the Begin/EndInit block does it work? – Steve Oct 15 '18 at 15:09
  • Have you tried the ToolBox version of the Control, dropping one on a Form? Inspecting the generated code could be useful. The pattern should be the same used by other controls that implement `ISupportInitialize` (e.g., `PictureBox`, `TrackBar`...). – Jimi Oct 15 '18 at 15:28
  • @Steve: I moved the Add() out of the Begin/EndInit. It still hangs. – Robert Deml Oct 15 '18 at 15:29
  • @Jimi: the InitializeComponent() calls EndInit() and never returns. It has the same behavior as the non-Toolbox code. – Robert Deml Oct 15 '18 at 15:35
  • Have you tried the WinForms project included in the Library? Same result? Do you have Edge in that machine :) – Jimi Oct 15 '18 at 15:43
  • 1
    @Jimi: I tried to compile the source of the WebView Sample Form, but got some errors. I had to change the package reference of the WebView. But it also failed in EndInit, but with an exception: "The remote procedure call failed. (Excpetion from HRESULT: 0x800706BE)". – Robert Deml Oct 15 '18 at 15:51
  • COM Interop failed to instantiate an out-of-process component. Apparently, it can't create the instance. A System update is required? [Crash in Wpf.WebView: The remote procedure call failed. HRESULT: 0x800706BE](https://github.com/windows-toolkit/WindowsCommunityToolkit/issues/2328) – Jimi Oct 15 '18 at 16:04

3 Answers3

2

I am running Visual Studio as Administrator.

If I run VS normally, the WebView works.

So, now I have a different problem: How do I run a WebView as Administrator?

Thank you to Steve and Jimi for the helpful comments.

Robert Deml
  • 12,390
  • 20
  • 65
  • 92
0

Looks like you'll need to download the latest version v5 from the below

https://github.com/windows-toolkit/WindowsCommunityToolkit/releases/tag/v5.0.0

4.0.3 is now obsolete, looks like a Windows 10 update broke the APIs. The above link is for Windows 10 Build 17110 or later

McNline
  • 178
  • 1
  • 10
0

Long story short, you can't run the WebView component in elevated mode (as an Administrator). If you do, it create event viewer errors:

The other side effect is that it hangs in the EndInit method.

Garet Jax
  • 1,091
  • 3
  • 17
  • 37