include76 / Diga.WebView2

WebView2 Wrapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Diga.WebView2

WebView2 Wrapper For => Edge Chromium

Microsoft WebView2

Nuget Packages:

There are NUGET-PACKAGES

  • Diga.WebView2.Interop => NuGet
  • Diga.WebView2.Wrapper => NuGet
  • Diga.WebView2.WinForms => NuGet
  • Diga.WebView2.Wpf => NuGet

WebView2 Runtime

Why this project was created.

Microsoft provides a WebControl for Windows forms. However, this is based on Internet Explorer. This cannot represent various features. This does not support WebAssemblies. The motivation for this project, however, was to load and display WebAssembly applications (BLAZOR-SITES). It should also be possible to work completely without an HTTP server.

Addendum:

From version 0.9.515-prerelease there will be a WinForms control included. (finally).

Currently, however, it is not possible to create a real Any-CPU version with this control. If Any-CPU excludes the hook (pefere x86) on a 64bit machine, a run-time error occurs. This error occurs when the WebView2 NuGet-Package is bound. See => Why is the "microsoft.web.webview2" package not linked.

Microsoft and WebView2

Microsoft provides Edge Chromium, a modern browser for Windows. With the COM-based interface of WebView2, this can also be programmed. Unfortunately, this option is officially reserved only for C++ programmers. This is to be fixed with this project.

packages are currently supported.

microsoft.web.webview2

  • Version 1.0.992.28 is the current Version on Client - PC's
  • Version 1.0.1020.30 is the current Version on Client - PC's

WebView2 Release-Notes

Why is the microsoft.web.webview2 package not linked?

The packages are not linked because this does not allow the mapping of Any CPU. The packages are always added only based on the setting of the projects. The API calls in this project are designed to draw the correct DLL depending on CPU usage. Therefore, the DLL's were included in the project. The NATIVEN-DLL's can then be found in the bin directory under native/x86 or native/x64.

Name of the project files.

To enable quick switching between WebView2 versions, the namespaces are kept the same. This is how it should be possible to do so later in the goal- project, simply on the basis of the package to decide which version of WebView2 to use.

  • Diga.WebView2.Interop. {Framework}. {PakteVersion}

  • Diga.WebView2.Wrapper. {Framework}. {PaketVersion}

  • Diga.WebView2.WinForms. {Framework}. {PaketVersion}

  • Since version V106643 a name extension is no longer added.

Framework:

  • Std => Standard 2.0, Standard 2.1
  • Core => Dotnet core 3.1, 5.0, 6.0
  • Framework => NET framework 4.7.2

Package version:

This means the version of the WebView2 packages.

How were the interop sources created?

The Microsoft microsoft.web.webview2 package contains webview2.tlb. The basic file was created with tlbImp.exe. Because the resulting DLL does not correctly reflect interfaces, they need to be revised. Therefore, the DLL, with JetBrains-DotPeek, has been converted to sources and adjusted accordingly.

Why a separation between Interop, Wrapper and WinForms

The separation is maintained because the packages can be useful in different projects. So it may be that only the interop and wrapper packages are used in some projects. Or it may be that only the Interop package is needed.

Why I get an error when I try to add the core control in a WinForms application in the designer.

This seems to be related to Visual Studio.

AddRemoteObject => COM interop

It is possible to pass a dot-net object as a remote object to the web browser. In my tests, I was able to set and read properties. However, I did not manage to call functions without errors. Neither with parameters, nor without and not with and without return. < V9430. AddRemoteObject works fine when you use >=V9430. If you add the followin Rremote Object first:

    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class HostObjectHelper
    {
        private const int DISP_E_MEMBERNOTFOUND = -2147352573;
        private const int DISP_E_TYPEMISMATCH = -2147352571;

        /// <summary>Check whether a member is a method of an object.</summary>
        /// <param name="obj">The host object to check.</param>
        /// <param name="name">The name of the member to check.</param>
        public bool IsMethod(object obj, string name)
        {
            Type type = obj.GetType();
            if (!type.IsClass || type.IsCOMObject)
                throw new COMException((string) null, -2147352571);
            if (type.GetMember(name).Length == 0)
                throw new COMException((string) null, -2147352573);
            foreach (MemberInfo memberInfo in type.GetMember(name))
            {
                if (memberInfo.MemberType == MemberTypes.Method)
                    return true;
            }
            return false;
        }
    }

It is important that the object is given the following name:

{60A417CA-F1AB-4307-801B-F96003F8938B} Host Object Helper

The object is now added automatically.

Windows Forms

If you use net Framework. You have to modify the Diga.WebView2.Interop.dll Reference.

Set Embed Interop Types to False! This copies the DLL into the program directory. If you do not make this setting, there will be an error when starting the application.

WinForms is STAThread. The access to the component must therefore also be thread-safe, as is usual in WinForms. You should never call properties or functions of a component directly from a Task. Use a delegate to do this, if necessary.

public void SendMessage(string message)
{
   if (this.webView1.InvokeRequired)
   {
      Action<string> ac = SendMessage;
      this.webView1.Invoke(ac, message);
   }
   else
   {
      this.webView1.SendMessage(message);    
   }
}

public async Task InvokeSendMessage(string msg)
{
   await Task.Run(() => this.SendMessage(msg));
}
This text was automatically translated with the Microsoft translator.

About

WebView2 Wrapper

License:MIT License


Languages

Language:C# 99.8%Language:HTML 0.1%Language:JavaScript 0.0%