我需要將用戶資料檔案夾設定為我選擇的自定義路徑,以便對其進行控制。在微軟的檔案中,解釋了可以在環境初始化之前使用 CoreWebView2Environment 類設定該選項。
但這適用于 C#,而不是 WinForms VB.NET (.Net Framework)。命名空間中甚至沒有一個CoreWebView2Environment,只有CoreWebView2.Environment一個沒有相同的功能,但似乎有一個將路徑作為只讀字串回傳的函式。
我找不到有關此類的任何檔案。有誰知道這是否可以做到?
uj5u.com熱心網友回復:
要顯式初始化CoreWebView2,請嘗試以下操作:
添加以下 Imports 陳述句:
Imports Microsoft.Web.WebView2.CoreImports Microsoft.Web.WebView2Imports System.IO
初始化CoreWebView2Async:
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
注意:如果想要顯式初始化CoreWebView2,必須在設定控制元件Source屬性之前完成WebView2。
用法:
Await InitializeCoreWebView2Async(Path.Combine("C:\Temp", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name))
如果您在表單中呼叫它(例如:Form1.vb),那么您將執行以下操作:
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:
- CoreWebView2 Class
- CoreWebView2CreationProperties
- CoreWebView2Environment Class
- CoreWebView2Environment.CreateAsync
- WebView2.EnsureCoreWebView2Async(CoreWebView2Environment) Method
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/454284.html
