在wpf中使用webview2来加载网页是出错
相关代码
private void InitializeBrowserControl()
……
if (webView2RuntimeFound == true)
{webView2.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;
System.Windows.MessageBox.Show("运行到1");
webView2.EnsureCoreWebView2Async();
System.Windows.MessageBox.Show("运行到2");
}
……
}
private void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{System.Windows.MessageBox.Show("Initialization开始");
webView2.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
webView2.CoreWebView2.Settings.IsStatusBarEnabled = false;
webView2.CoreWebView2.Settings.AreDevToolsEnabled = Debugger.IsAttached;
webView2.Visibility = Visibility.Visible;
System.Windows.MessageBox.Show("Initialization中间");
if (null != AutoUpdater.BasicAuthChangeLog)
{
webView2.CoreWebView2.BasicAuthenticationRequested += delegate (
object sender,
CoreWebView2BasicAuthenticationRequestedEventArgs args)
{
args.Response.UserName = ((BasicAuthentication)AutoUpdater.BasicAuthChangeLog).Username;
args.Response.Password = ((BasicAuthentication)AutoUpdater.BasicAuthChangeLog).Password;
};
}
webView2.CoreWebView2.Navigate(_args.ChangelogURL);
System.Windows.MessageBox.Show("Initialization结尾");
}
问题是:始终显示
EnsureCoreWebView2Async cannot be used before the application's event loop has started running.
如果是在winform中使用,则没有问题,能先执行完webView2.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted,也就是会显示"Initialization开始"、"Initialization中间"、"Initialization结尾"的弹窗,随后能显示“运行到1”,但不能显示“运行到2”
但是在wpf中则似乎没有执行webView2.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;
,也就是不示"Initialization开始"、"Initialization中间"、"Initialization结尾"的弹窗,能显示“运行到1”,但不能显示“运行到2”,而是报错EnsureCoreWebView2Async cannot be used before the application's event loop has started running.
我的解答思路和尝试过的方法
用await 也不行
我想要达到的结果:不再报错;
百度、csdn、等平台 能搜索的答案都试过了……
太奇怪了