问题描述
我已经下载并附加了 WebView2 的 FixedVersionRuntime.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);
}
然后我在此之后设置源:
await InitializeAsync();
me.source = new Uri(((Mainviewmodel)this.DataContext).Config.DefaultURL);
当使用常青安装程序时,它运行良好,但在移动到固定版本时,它在部署时似乎无法正确加载。
解决方法
我已经测试了以下内容,这似乎有效:
示例
给定:
- WebView2 固定版本:Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86.cab
- 项目文件夹:C:\Projects\WpfTestFixedVersion
- 输出文件夹:C:\Projects\WpfTestFixedVersion\WpfTestFixedVersion\bin\Debug
项目编译使用:
- 配置:调试
- 平台:任何 CPU(首选 32 位)
从 .cab 中提取文件
- 打开一个 cmd 窗口
cmd 窗口
C:\Users\Test\Downloads> expand Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86.cab -F:* "C:\Projects\WpfTestFixedVersion\WpfTestFixedVersion\bin\Debug"
注意:在上述命令中使用expand
时,目标文件夹必须已经存在并且名称不能以'\'结尾。
C:\Projects\WpfTestFixedVersion\WpfTestFixedVersion\bin\Debug
C:\Projects\WpfTestFixedVersion\WpfTestFixedVersion\bin\Debug\Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86
选项 1:
InitializeAsync
public async Task InitializeAsync()
{
string installPath = @".\Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86";
var webView2Environment = await CoreWebView2Environment.CreateAsync(installPath);
await browserControl.EnsureCoreWebView2Async(webView2Environment);
}
选项 2:
注意:此选项允许您指定 userDataFolder。如果未指定,则使用用户的临时文件夹作为 userDataFolder 的位置。
InitializeAsync
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",options);
//webView2Environment = await CoreWebView2Environment.CreateAsync(null,options);
//wait for CoreWebView2 initialization
await wv.EnsureCoreWebView2Async(webView2Environment);
}