I am creating a object of Microsoft.Web.WebView2.WinForm.WebView2 but the sub obect of this coreWebView2 is null
Microsoft.Web.WebView2.WinForm.WebView2 webView = new Microsoft.Web.WebView2.WinForm.WebView2()
// Change some GUI properties of webView
webView.CoreWebView.NavigateUrl(url)
// I can not access the above line because CoreWebView is null
As @Abbas Tambawala said in a comment, you have to make sure that your project is set for a specific bitness. The sometimes default "AnyCPU" setting will seem to work but the control won't initialize,
await webView.EnsureCoreWebView2Async
will return a Task that never completes,CoreWebView2
will be null, and other symptoms will be a clue to check that setting in your project.Also, be aware that if you are working on a solution with multiple projects, like I was... you need to make sure all the projects are consistently flagged correctly.
The DLL project I was working on was marked as X86, but the project that loaded my DLL was marked with "AnyCPU". Many moments of WTF is going on... the comment about "prefer 32 bit" to Ryan's answer gave the "Ah-HA" moment.
Also, you may want to check what event or method you are setting webView2.Source or calling EnsureCoreWebView2Async in... webView2 needs the UI thread to be completely initialized and the form shown. I generally put my webView2 init code in the Form.Shown event. Here is a link describing the Form Events order: MS-Docs Form Events