To explicitly initialize CoreWebView2
, try the following:
Add the following Imports statements:
Imports Microsoft.Web.WebView2.Core
Imports Microsoft.Web.WebView2
Imports System.IO
InitializeCoreWebView2Async:
Private Async Function InitializeCoreWebView2Async(Optional userDataFolder As String = "") As Task
Dim options As CoreWebView2EnvironmentOptions = Nothing
Dim cwv2Environment As CoreWebView2Environment = Nothing
'it's recommended to create the userDataFolder in the same location
'that your other application data is stored (ie: in a folder in %APPDATA%)
'if not specified, we'll create a folder in %TEMP%
If String.IsNullOrEmpty(userDataFolder) Then
userDataFolder = Path.Combine(Path.GetTempPath(), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name)
End If
'create WebView2 Environment using the installed or specified WebView2 Runtime version.
'cwv2Environment = Await CoreWebView2Environment.CreateAsync("C:\Program Files (x86)\Microsoft\Edge Dev\Application\1.0.1054.31", userDataFolder, options)
cwv2Environment = Await CoreWebView2Environment.CreateAsync(Nothing, userDataFolder, options)
'initialize
Await WebView21.EnsureCoreWebView2Async(cwv2Environment)
End Function
Note: If one desires to explicitly initialize CoreWebView2
, it must be done prior to setting the Source
property for the WebView2
control.
Usage:
Await InitializeCoreWebView2Async(Path.Combine("C:\Temp", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name))
If you're calling this in a form (ex: Form1.vb), then you'd do the following:
Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
System.Diagnostics.Debug.WriteLine("MS Edge Version: " & CoreWebView2Environment.GetAvailableBrowserVersionString())
'initialize
'Await InitializeCoreWebView2Async()
Await InitializeCoreWebView2Async(Path.Combine("C:\Temp", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name))
'ToDo: add desired code, such as navigating to a URL
End Sub
Note: CoreWebView2CreationProperties may be of interest as well.
Resources: