我已经下载并附加了 WebView2 的 FixVersionRuntime.88.0.705.81.x64 并将其附加到我的项目中。
使用以下内容应该加载必要的页面,但加载 WebView 时不会崩溃,但不会加载任何页面:
public async Task InitializeAsync()
{
string installPath = @"C:\Program Files (x86)\WebView2Runtime\Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x64\";
var webView2Environment = await CoreWebView2Environment.CreateAsync(installPath);
await browserControl.EnsureCoreWebView2Async(webView2Environment);
}
Run Code Online (Sandbox Code Playgroud)
然后我在此之后设置源:
await InitializeAsync();
me.Source = new Uri(((MainViewModel)this.DataContext).Config.DefaultURL);
Run Code Online (Sandbox Code Playgroud)
使用常绿安装程序时,它工作正常,但当移动到固定版本时,部署时似乎无法正确加载。
我已经测试了以下内容,这似乎有效:
例子
鉴于:
项目编译使用:
从 .cab 中提取文件
命令窗口
C:\Users\Test\Downloads> expand Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86.cab -F:* "C:\Projects\WpfTestFixedVersion\WpfTestFixedVersion\bin\Debug"
Run Code Online (Sandbox Code Playgroud)
注意:在上述命令中使用时expand,目标文件夹必须已经存在,并且名称不能以“\”结尾。
C:\Projects\WpfTestFixedVersion\WpfTestFixedVersion\bin\Debug
C:\Projects\WpfTestFixedVersion\WpfTestFixedVersion\bin\Debug\Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86
选项1:
初始化异步
public async Task InitializeAsync()
{
string installPath = @".\Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86";
var webView2Environment = await CoreWebView2Environment.CreateAsync(installPath);
await browserControl.EnsureCoreWebView2Async(webView2Environment);
}
Run Code Online (Sandbox Code Playgroud)
选项 2:
注意:此选项允许指定 userDataFolder。如果未指定,它将使用用户的临时文件夹作为 userDataFolder 的位置。
初始化异步
public async Task InitializeAsync(WebView2 wv, string webCacheDir = "")
{
CoreWebView2EnvironmentOptions options = null;
string tempWebCacheDir = string.Empty;
CoreWebView2Environment webView2Environment = null;
//set value
tempWebCacheDir = webCacheDir;
if (String.IsNullOrEmpty(tempWebCacheDir))
{
//get fully-qualified path to user's temp folder
tempWebCacheDir = System.IO.Path.GetTempPath();
tempWebCacheDir = System.IO.Path.Combine(tempWebCacheDir, System.Guid.NewGuid().ToString("N"));
}
//use with WebView2 FixedVersionRuntime
webView2Environment = await CoreWebView2Environment.CreateAsync(@".\Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86", tempWebCacheDir, options);
//webView2Environment = await CoreWebView2Environment.CreateAsync(@"C:\Program Files (x86)\Microsoft\Edge Dev\Application\90.0.810.1", tempWebCacheDir, options);
//webView2Environment = await CoreWebView2Environment.CreateAsync(null, tempWebCacheDir, options);
//wait for CoreWebView2 initialization
await wv.EnsureCoreWebView2Async(webView2Environment);
}
Run Code Online (Sandbox Code Playgroud)