Reputation: 21
Will WebView2 work with "Visual Studio 2017" - either VB or C# - for .NET 4.7.2 - winforms
NOTE CORE, as its .net 4.7.2 so I can't do all the "await" code that is required I don#'t think.
Is there or does someone have any working small same code showing how to get webview2 to work in the above setup?
Again, c# or vb, .net 4.7.2 NOT CORE
Angrid
Upvotes: 0
Views: 1770
Reputation: 74740
I might get where you're coming from with the question, actually.. This was painful
I have canary channel edge dev installed, check.. VS2017 and 2019, check..
I had a play around with WebView2 in a new WinForms 472 Project in VS 2017 - the only way I could get the nuget package to add a usable Microsoft.Web.WebView2.WinForms
reference/namespace was to add a prerelease package. Addining a non prerel nuget showed nothing in the references
After adding the latest pre-rel package I founf that adding the control to the form and setting the Source (in code or designer) merely elicited a NullReferenceException when the source was being set
I followed the instructions in the Getting Started documentation to the letter, but I still got the NullReferenceException as soon as any form of interaction took place with the control
I opened the official winforms example in VS2017 and got an an error that the project file couldn't be loaded because "Project file is incomplete. Expected imports are missing."
Switched to opening the official sample in VS 2019, resolved the complaint about not having targeting pack 461 installed by editing the csproj to a pack I did have, and reloading the project.. It builds and runs.. But it's .NET core
A lot more messing around (always hitting the WebView2 NullReferenceException as soon as a property was set on it, or it was added to the form's controls collection), and I eventually (by chance) caught this tooltip for the WebView2 constructor:
Something you don't really see if the forms designer did the constructing..
So once I'd set up some code like:
public partial class Form1 : Form
{
private WebView2 x = new WebView2();
public Form1()
{
InitializeComponent();
}
private async void Form1_Load(object sender, EventArgs e)
{
await x.EnsureCoreWebView2Async();
x.Dock = DockStyle.Fill;
x.Source = new Uri("https://stackoverflow.com");
this.Controls.Add(x);
}
}
I started hitting a BadImageFormatException - that was resolved by setting the app to be x64 instead of AnyCPU/X86
And finally it works, in WinForms:
What a fuss..
TLDR:
Ensure...
seeminglt must be the first thin you do after you construct the controlUpvotes: 0