rustic_erphone 2022-07-25 20:29 采纳率: 0%
浏览 261

wpf 中使用webview2的问题

在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、等平台 能搜索的答案都试过了……
太奇怪了

  • 写回答

3条回答 默认 最新

  • rustic_erphone 2022-07-25 21:29
    关注

    异常信息

    System.InvalidOperationException
      HResult=0x80131509
      Message=EnsureCoreWebView2Async cannot be used before the application's event loop has started running.
      Source=Microsoft.Web.WebView2.Wpf
      StackTrace:
       at Microsoft.Web.WebView2.Wpf.WebView2.EnsureCoreWebView2Async(CoreWebView2Environment environment, CoreWebView2ControllerOptions controllerOptions)
       at AutoUpdaterDotNET.UpdateForm.<InitializeBrowserControl>d__6.MoveNext() in D:\Software\My_Program\C#\试题排版工具\2022716日\Niu Niu edit\Niu Niu edit\autoUpdate\Window\UpdateForm.xaml.cs:line 122
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in f:\dd\ndp\clr\src\BCL\system\runtime\exceptionservices\exceptionservicescommon.cs:line 133
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) in f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 980
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) in f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 928
       at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() in f:\dd\ndp\clr\src\BCL\system\threading\threadpool.cs:line 1252
       at System.Threading.ThreadPoolWorkQueue.Dispatch() in f:\dd\ndp\clr\src\BCL\system\threading\threadpool.cs:line 820
    

    此异常最初是在此调用堆栈中引发的:

    
     Microsoft.Web.WebView2.Wpf.WebView2.EnsureCoreWebView2Async(Microsoft.Web.WebView2.Core.CoreWebView2Environment, Microsoft.Web.WebView2.Core.CoreWebView2ControllerOptions)
        AutoUpdaterDotNET.UpdateForm.InitializeBrowserControl() (位于 UpdateForm.xaml.cs 中)
        System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() (位于 exceptionservicescommon.cs 中)
        System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, object, bool) (位于 executioncontext.cs 中)
        System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, object, bool) (位于 executioncontext.cs 中)
        System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() (位于 threadpool.cs 中)
        System.Threading.ThreadPoolWorkQueue.Dispatch() (位于 threadpool.cs 中)
    
    评论

报告相同问题?

问题事件

  • 创建了问题 7月25日

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据