我的.NET WebView2控件有问题。我以为我把它修好了,但它不起作用。我读了很多帖子,但都无济于事。
我有一个运行在服务器上的WPF C#应用程序。不同的人通过网络浏览器登录服务器并运行应用程序。
在这个应用程序中,我打开了一个WebView2浏览器,将用户数据目录设置为每个人的唯一目录。
当我设置用户数据目录并调用EnsureCoreWebView2Async((时,我在异常代码"中得到一个错误;访问被拒绝。(HRESULT中的异常:0x80070005(E_ACCESSDENIED((。
以下是代码:
public static async void InitializeWebView(WebView2 browser, string path)
{
Directory.CreateDirectory(path);
browser.CreationProperties = new CoreWebView2CreationProperties()
{
UserDataFolder = path
};
try
{
await browser.EnsureCoreWebView2Async();
}
catch( Exception ex)
{
Log.LogString("Ensure error: " + ex.Message);
}
}
我尝试过各种各样的事情都没有成功。我做错了什么?有什么建议吗?
我对CoreWebView2CreationProperties不是很熟悉,但根据文档。
它的主要目的是设置为CreationProperties以便自定义WebView2在隐式期间使用的环境初始化。。。如果您需要对WebView2控件使用的环境进行完全控制,那么您需要通过CreateAsync(String、String、CoreWebView2EnvironmentOptions(创建自己的环境并将其传递给EnsureCoreWebView 2Async(CoreWebView2Environment(来显式初始化控件,然后将Source属性设置为任何属性。
正如上面引用的文档中所提到的,当设置了Source
属性并且CoreWebView2
尚未显式初始化时,会发生隐式初始化。
要显式初始化CoreWebView2
,请尝试以下操作:
public async Task InitializeCoreWebView2Async(WebView2 wv, string userDataFolder = null)
{
//initialize CoreWebView2
CoreWebView2EnvironmentOptions options = null;
CoreWebView2Environment cwv2Environment = null;
//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))
userDataFolder = Path.Combine(Path.GetTempPath(), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
//create WebView2 Environment using the installed or specified WebView2 Runtime version.
//cwv2Environment = await CoreWebView2Environment.CreateAsync(@"C:Program Files (x86)MicrosoftEdge DevApplication1.0.1054.31", userDataFolder, options);
cwv2Environment = await CoreWebView2Environment.CreateAsync(null, userDataFolder, options);
//initialize
await wv.EnsureCoreWebView2Async(cwv2Environment);
System.Diagnostics.Debug.WriteLine("UserDataFolder: " + userDataFolder);
}
注意:如果希望显式初始化CoreWebView2
,则必须在为WebView2
控件设置Source
属性之前进行。
用法:
await InitializeCoreWebView2Async(webView21, Path.Combine(@"C:Temp", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name));
资源:
- CoreWebView2类
- 核心WebView2创建属性
- CoreWebView2环境类
- CoreWebView2Environment.CreateAsync
- WebView2.EnsureCoreWebView2Async(CoreWebView 2环境(方法
旧线程,但我遇到了同样的问题。。。并从中找到了一个简单的解决方案https://github.com/microsoft/microsoft-ui-xaml/issues/7190。。。在创建webview之前设置它所使用的环境变量。
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "AppName");
Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", path);
InitializeComponent();
感谢您的持续帮助。由于性格限制,我在回答部分回答您的问题。针对您的评论。。。
如果Source是在初始化之前设置的,我会得到一个不同的错误(我过去也遇到过(。
以下是WebView2控件的xaml:
<Wpf:WebView2 Name="Browser" Margin="20,20,20,20" CoreWebView2InitializationCompleted="Browser_CoreWebView2InitializationCompleted" />
正如您所看到的,我订阅了InitializationComplete事件,由于错误,该事件没有被调用。
以下是承载WebView2控件的表单的来源:
private async void _DoIt()
{
try
{
var env = await CoreWebView2Environment.CreateAsync(null, Global._GetServerDataDirectory(_Person));
await Browser.EnsureCoreWebView2Async(env);
}
catch( Exception ex)
{
Log.LogString("DoIt error: " + ex.Message);
}
}
public PayPal(Person person)
{
InitializeComponent();
_Person = person;
_DoIt();
Log.LogString("After PayPal constructor");
}
_DoIt((中出现错误。
好吧,经过数小时的追踪,最终问题是Thinfinity的VirtualUI产品不支持使用WebView2的应用程序。这是我的核心问题,已经得到了供应商的确认。
再次感谢大家在这方面的友好帮助。