MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
444 stars 53 forks source link

Could not load type 'System.Windows.Forms.ContextMenu' from assembly 'System.Windows.Forms, Version=6.0.2.0 #3189

Open KennethEvans opened 1 year ago

KennethEvans commented 1 year ago

Description I am to use trying to use WebView2 for the first time in a WinForms application. I have installed NuGet Microsoft.Web.WebView2 109.1518.46. Microsoft Edge WebView2 Runtime 109.1518.78 is installed.

When selecting WebView2 from the Toolbox in Designer I get:

[17:28:44.233610] fail: Request failures: DesignerHosts/CreateComponent.
                        Microsoft.DotNet.DesignTools.Client.DesignToolsServerException: Could not load type 'System.Windows.Forms.ContextMenu' from assembly 'System.Windows.Forms, Version=6.0.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

A screenshot of the error dialog: Screenshot-devenv-23-02-05_17-32

I cannot exit Visual Studio 2022 unless I say to not save the solution. I have tried several times, using 1.0.1462.37 and a completely new Form.

The project is an existing library targeting net6.0-windows;net48. I am trying to replace the use of WebBrowser.

There is no use of ContextMenu in the solution. I understand it has long been replaced by ContextMenuStrip, which is not used in this Form.

Version SDK: 109.1518.46 Runtime: 109.1518.78 OS: Win10, 19045.2486

novac42 commented 1 year ago

@KennethEvans Thanks for the report. We will take a look into this and get back to you.

GeorgRottensteiner commented 1 year ago

Probably a similar cause, I also have a project building for .NET 3.5, 4.8 and 6.0. Using Windows Forms. Since a few days I can't open a form in the designer with the exact same message. There is no use of ContextMenu anywhere in the project (6.0 wouldn't build with it).

Deleting the .vs folder, restarting VS (2022) or toggling .NET versions in the file choice box doesn't change the outcome.

Edit: I found the culprit in my case: The designer seems to use the order of the framework versions given in the project properties. When I moved .net6.0-windows to the back, the designer worked again (after a restart of VS).

This strongly hints as this being a VS 2022/Forms designer issue rather then WebView2.

KennethEvans commented 1 year ago

@GeorgRottensteiner: That change did not fix it for me.

I now have for Target Frameworks: net6.0-windows;net48

I have deleted .vs and .vshistory (was not loading properly after the change) and restarted VS several times.

Clicking on WebView2(Microsoft.WebView2.WinForms) gives

Screenshot-devenv-23-05-09_11-40

GeorgRottensteiner commented 1 year ago

You have to put the net6.0-windows at the back again. VS 2022 seems to take the first given framework and use it for the designer. Once it has decided which version to use it is stuck with that version, until a restart (this is a guess by me).

From my understanding System.Windows.Forms = 6.0.2.0 is the .net 6.0 version, which simply doesn't have ContextMenu.

In the end it's a workaround that worked for my specific system, maybe I just got lucky.

KennethEvans commented 1 year ago

Ok, I changed it back to net48,net6.0-windows. For some reason this screws up VSHistory.

Also, I upgraded Microsoft.Web.WebView 2 1.0.1518 to 1.0.1774.30.

Now I can't open the file in Design mode at all. Haven't figured out how to fix this. I have deleted .vs again. That didn't help.

Screenshot-devenv-23-05-09_12-40

KennethEvans commented 1 year ago

I have got WebView2 to work in a project just targeting net 48 only.

KennethEvans commented 1 year ago

Back to the original problem. I have started over from a backup to get rid of the Designer error shown above and then upgraded Microsoft.Web.WebView 2 1.0.1518 to 1.0.1774.30.

I still get the dialog when I click on WebView2 in the Toolbox.

@GeorgRottensteiner: I am confused at what you did. I read you moved .Net 6 to the back, i.e. net48,net6.0-windows. (I don't believe WebView works for .Net 3.5). And then you changed to back to net48,net6.0-windows?

This doesn't make sense. I must have missed something.

KennethEvans commented 1 year ago

I made a .Net 6 class library from scratch. To allow it to use Windows Forms I modified the project file:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFrameworks>net6.0-windows</TargetFrameworks>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1774.30" />
  </ItemGroup>
</Project>

I could then add a Form1. And I could add a WebView to it.

There was no way in the UI to make it target two frameworks, so I replaced TargetFrameworks with

   <TargetFrameworks>net6.0-windows;net48</TargetFrameworks>

Without doing anything else I got the error on WebVew2 in the Toolbox.

I can use Webview2 in a .Net 6 project or a .Net Framework 4.8 project. There seems to be some problem combining them, however.

It would be nice if someone on the WebView2Feedback team would respond to this.

Shangminx commented 1 year ago

Hi @KennethEvans , Now the error msg is this right?

Ok, I changed it back to net48,net6.0-windows. For some reason this screws up VSHistory.

Also, I upgraded Microsoft.Web.WebView 2 1.0.1518 to 1.0.1774.30.

Now I can't open the file in Design mode at all. Haven't figured out how to fix this. I have deleted .vs again. That didn't help.

Screenshot-devenv-23-05-09_12-40

I only have vs2019 now. I test it locally for net5.0-windows;net48. It's work. Will keep looking this issue.

KennethEvans commented 1 year ago

The error message is the dialog shown above. It has a stack trace. If I were working on this, I would find where that is generated. It is bogus because there is no context menu.

Shangminx commented 1 year ago

@KennethEvans , Could you help to provide your project code. It's useful for us to repro this issue.

KennethEvans commented 1 year ago

Unfortunately not. I got my class library to work by copying the Form to one of the test projects mentioned above where WebView does work ok in the Toolbox. (The working projects only have one TargetFramework.) I added the WebView2 there and copied the Form files back to the class library. There was then no need to use the Toolbox. Surprisingly the Toolbox works ok now in the class library. At least there is no error dialog. I have no need to use it further.

I did not commit the version that was not working following the steps mentioned in my post of May 9, and so have nothing you could use there. The project as committed now does not have the problem.

The projects is at https://github.com/KennethEvans/VS-Utils. You could try starting with the commit on May 10 (427327c) which would put you where I was when I tried to convert to WebView2 as described in the original post in this topic. The Form in question is ScrolledHTMLDialog. It is a relatively simple use of WebBrowser which I was then trying to change to WebView2. I didn't do much more than add the Microsoft.Web.WebVew2 NuGet package to get to the problem described with the Toolbox.