【问题标题】:Unable to load web page using the CefSharp embedded Chromium browser control?无法使用 CefSharp 嵌入式 Chromium 浏览器控件加载网页?
【发布时间】:2015-01-17 21:06:53
【问题描述】:

我正在艰难地开始它。首先,我尝试使用 NuGet 包。当我安装它们时,对 CefSharp DLL 的必要引用都没有出现在我的引用列表中。所以我通过搜索 /packages 子目录手动添加了它们。在构建我的项目后,我发现当我执行 URL 加载时没有发生任何事情。没有加载网页,没有错误消息,没有触发异常。什么都没有。

我完全删除了 CefSharp Nuget 包,并从 GitHub 下载了最新的 repo 源。我首先确保 CefSharp.WinForms.Example 项目中的 URL 加载工作正常。效果很好。然后,我将 CefSharp 解决方案中的必要项目集复制到我的项目解决方案中,直到我的解决方案正确构建。我从 CefSharp.WinForms.Example 项目中添加了 BrowserForm 表单,以确保它不是我的代码。但是,当我在运行时打开该表单时,在地址栏中输入一个 URL,然后单击 Go 按钮没有任何反应。和以前一样。

我在ManagedCefBrowserAdapter.h中追溯了代码,发现了这个C++方法的故障点:

    void LoadUrl(String^ address)
    {
        _address = address;
        auto cefFrame = _renderClientAdapter->TryGetCefMainFrame();

        // This is where cefFrame is treated qual to nullptr despite 
        //  having the value of {CefRefPtr<CefFrame>}, thereby defeating
        //  the call to LoadURL().
        if (cefFrame != nullptr)
        {
            cefFrame->LoadURL(StringUtils::ToNative(address));
        }
    }

原来有一个非常奇怪的问题。当我将代码跟踪到“if”语句时,我看到 cefFrame 的值是:

{CefRefPtr<CefFrame>}

但是,它显然不是 NULL,但它被认为是。 (或者我对 {CefRefPtr} 值的分析是错误的,我的分析也是如此。)

在任何一种情况下,“if”语句都将 cefFrame 的当前值视为等于 nullptr 并跳过调用 LoadURL()cefFrame 引用对象上的 /em> 方法。我在我的 GitHub CefSharp 存储库的解决方案副本中跟踪了完全相同的代码,确实进入了“if”块并执行 LoadURL() 方法。我检查了 cefFrame 的值,它与我的应用程序中的值相同。由于某些真正奇怪的原因,这里或我的应用程序中存在更隐蔽的问题,或者它确实将 cefFrame{CefRefPtr} 值视为与 相同nullptr 的什么时候不应该。

如果有人知道如何在我的代码中工作,我将不胜感激。

更新:我进行了三重检查,并且在 GitHub CefSharp 解决方案有效和我的项目无效之间,构建配置是相同的。一切都设置为 x86,除了设置为 Win32 的核心项目(在这两种情况下)。

我刚刚进行了重建,除此之外,我浏览了每个包含的项目并手动完全删除了 /bin 和 /obj 目录。现在,当我运行该项目时,正在发生一些不同的事情。每当我的代码尝试创建一个新的 BrowserTabUserControl 时,我都会得到一个未处理的异常:

System.IO.FileNotFoundException was unhandled
  _HResult=-2147024770
  _message=Could not load file or assembly 'CefSharp.Core.dll' or one of its dependencies. The specified module could not be found.
  HResult=-2147024770
  IsTransient=false
  Message=Could not load file or assembly 'CefSharp.Core.dll' or one of its dependencies. The specified module could not be found.
  Source=HiveMind3D_proto
  FileName=CefSharp.Core.dll
  FusionLog=""
  StackTrace:
       at HiveMind3D_proto.BrowserTabUserControl..ctor(String url)
       at HiveMind3D_proto.BrowserForm.AddTab(String url, Nullable`1 insertIndex) in c:\Users\Robert\Documents\Visual Studio 2012\Projects\Windows Forms\HiveMind3D_proto\HiveMind3D_proto\BrowserForm.cs:line 35
       at HiveMind3D_proto.BrowserForm..ctor() in c:\Users\Robert\Documents\Visual Studio 2012\Projects\Windows Forms\HiveMind3D_proto\HiveMind3D_proto\BrowserForm.cs:line 24
       at HiveMind3D_proto.Program.Main() in c:\Users\Robert\Documents\Visual Studio 2012\Projects\Windows Forms\HiveMind3D_proto\HiveMind3D_proto\Program.cs:line 26
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

我尝试在主项目中删除 CefSharp.Core 项目引用,并通过重新添加对 CefSharp.Core 项目的引用来重新添加它。但我仍然得到那个例外。

【问题讨论】:

    标签: c# c++ chromium cefsharp nullptr


    【解决方案1】:

    好的,我现在正在跑步。尽管使用完整源代码而不是 NuGet 包。原来我需要做一些事情:

    根据常见问题解答:

    https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#Runtime_dependencies

    • 确保所有项目都设置为构建并构建为 x86 项目
    • 将以下文件复制到 /bin/Debug 和 /bin/Release 目录中。仅仅在主应用程序中引用它们是不够的:

      • libcef.dll

      • icudtl.dat

      • CefSharp.dll

      • CefSharp.WinForms.dll

    • 使用如下所示的代码在任意称为 Init() 的方法中初始化 Cef 库。确保在 Program.cs 中的 Main() 方法中首先调用 Init() 方法:

      public static void Init()
      {
          var settings = new CefSettings
          {
              LogSeverity = LogSeverity.Verbose,
              LogFile = "debug.log",
              RemoteDebuggingPort = 8088,
          };
      
          if (DebuggingSubProcess)
          {
              var architecture = Environment.Is64BitProcess ? "x64" : "x86";
              settings.BrowserSubprocessPath = "..\\..\\..\\..\\CefSharp.BrowserSubprocess\\bin\\" + architecture + "\\Debug\\CefSharp.BrowserSubprocess.exe";
          }
      
          settings.RegisterScheme(new CefCustomScheme
          {
              SchemeName = CefSharpSchemeHandlerFactory.SchemeName,
              SchemeHandlerFactory = new CefSharpSchemeHandlerFactory()
          });
      
          if (!Cef.Initialize(settings))
          {
              if (Environment.GetCommandLineArgs().Contains("--type=renderer"))
              {
                  Environment.Exit(0);
              }
              else
              {
                  return;
              }
          }
      }
      

    注意,最好将日志文件的详细程度设置为 Verbose,这样您就可以在日志文件中看到任何错误。他们可以成为救生员。

    【讨论】:

    • 当您说“确保所有项目都设置为构建并构建为 x86 项目”时,您是指所有引用 cefsharp 的项目吗?还是您的意思是解决方案中的所有项目?我有一个包含约 150 个项目的解决方案,我无法将它们全部随机更改为 x86。此外,“主动解决方案平台”的名称只是一个名称。如果我称它为“x86CefSharp”,但将项目平台设置为 x86,这应该可行吗?
    猜你喜欢
    • 2015-06-19
    • 2014-04-22
    • 1970-01-01
    • 2017-08-16
    • 2013-11-15
    • 2016-05-20
    • 2022-11-05
    • 1970-01-01
    • 2017-02-27
    相关资源
    最近更新 更多