gpt4 book ai didi

c# - WebView2 网站未加载

转载 作者:行者123 更新时间:2023-12-02 16:16:36 24 4
gpt4 key购买 nike

我已经下载并附加了 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);

当使用 evergreen 安装程序时,它工作正常,但当移动到固定版本时,它似乎在部署时无法正确加载。

最佳答案

我已经测试了以下,这似乎有效:

下载WebView2 Fixed Version

示例

给定:

  • WebView2固定版本:Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86.cab
  • 项目文件夹:C:\Projects\WpfTestFixedVersion
  • 输出文件夹:C:\Projects\WpfTestFixedVersion\WpfTestFixedVersion\bin\Debug

项目编译使用:

  • 配置:调试
  • 平台:任何 CPU(首选 32 位)

从 .cab 中提取文件

  • 打开命令窗口

命令窗口

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

enter image description here

C:\Projects\WpfTestFixedVersion\WpfTestFixedVersion\bin\Debug\Microsoft.WebView2.FixedVersionRuntime.88.0.705.81.x86

enter image description here

选项 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);
}

选项 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);

}

关于c# - WebView2 网站未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66500241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com