首页 > 解决方案 > 使用两个 cefsharp.winforms 实例关闭时,对象引用未设置为对象的实例

问题描述

我的 wpf 使用了两个 ChromiumWebBrowser(Cefsharp.Winforms) 实例,一个用于我的网站,一个用于显示 pdf 文件,当我关闭 MianWindow 时,会出现如下崩溃:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=CefSharp.WinForms
  StackTrace:
   在 CefSharp.WinForms.ChromiumWebBrowser.InternalDispose(Boolean disposing)
   在 CefSharp.WinForms.ChromiumWebBrowser.Dispose(Boolean disposing)
   在 System.ComponentModel.Component.Dispose()
   在 System.Windows.Forms.Control.Dispose(Boolean disposing)
   在 System.Windows.Forms.Integration.WinFormsAdapter.Dispose(Boolean disposing)
   在 System.ComponentModel.Component.Dispose()
   在 System.Windows.Forms.Integration.WindowsFormsHost.Dispose(Boolean disposing)
   在 System.Windows.Interop.HwndHost.WeakEventDispatcherShutdown.OnShutdownFinished(Object sender, EventArgs e)
   在 System.EventHandler.Invoke(Object sender, EventArgs e)
   在 System.Windows.Threading.Dispatcher.ShutdownImplInSecurityContext(Object state)
   在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Windows.Threading.Dispatcher.ShutdownImpl()
   在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   在 System.Windows.Application.RunDispatcher(Object ignore)
   在 System.Windows.Application.RunInternal(Window window)
   在 System.Windows.Application.Run(Window window)
   在 System.Windows.Application.Run()

我没有明确设置 CefSharpSettings.ShutdownOnExit,在我的关闭函数中,只需调用:

Application.Current.Shutdown();

如果为pdf删除一个就可以了。我在下面托管浏览器:

        <WindowsFormsHost Name="browserHost">
            <cefwpf:ChromiumWebBrowser x:Name="browser" IsBrowserInitializedChanged="browser_IsBrowserInitializedChanged">
            </cefwpf:ChromiumWebBrowser>
        </WindowsFormsHost>

我的 cefsharp 版本是 83.1.100 感谢您的帮助!

标签: cefsharp

解决方案


推荐阅读