WinForms dies in EndInit of Microsoft.Toolkit.Win32.UI.Controls.WinForms.WebView
Asked Answered
U

3

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?

Unsocial answered 15/10, 2018 at 14:53 Comment(7)
if you move the Controls.Add out of the Begin/EndInit block does it work?Sophistication
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...).Pitterpatter
@Steve: I moved the Add() out of the Begin/EndInit. It still hangs.Unsocial
@Jimi: the InitializeComponent() calls EndInit() and never returns. It has the same behavior as the non-Toolbox code.Unsocial
Have you tried the WinForms project included in the Library? Same result? Do you have Edge in that machine :)Pitterpatter
@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)".Unsocial
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: 0x800706BEPitterpatter
U
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.

Unsocial answered 15/10, 2018 at 16:21 Comment(0)
M
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

Mornay answered 7/12, 2018 at 15:24 Comment(3)
The last version v5.1.1 does not resolve the problem, I am also stuck that I can not run the application with the WebView as administratorScant
freedeveloper what win version are you on?Mornay
Windows 10 Enterprise 1809. Also the same in the last 1903 versionScant
S
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.

Standardize answered 30/7, 2019 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.