无法使用 CefSharp 嵌入式 Chromium 浏览器控件加载网页

Unable to load web page using the CefSharp embedded Chromium browser control?

本文关键字:浏览器 控件 加载 网页 Chromium 嵌入式 CefSharp      更新时间:2023-10-16
我刚开始的时候很

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

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

我跟踪了代码,并在 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>}

然而,它显然不是空的,但它却被当作是空的。 (或者我对 {CefRefPtr} 值的分析是错误的,我的分析也是错误的。

在任一情况下,"if" 语句都将 cefFrame 的当前值视为等于 nullptr,并跳过在 cefFrame 引用的对象上调用 LoadURL() 方法的行。 我在GitHub CefSharp存储库解决方案的副本中跟踪了完全相同的代码,确实输入了"if"块并执行LoadURL()方法。 我检查了 cefFrame 的值,它与我在应用程序中的值相同。 出于某种非常奇怪的原因,要么这里或我的应用程序中存在更阴险的问题,要么它确实将 cefFrame 的值 {CefRefPtr} 视为与 nullptr 的值相同,而它不应该这样做。

如果有人对如何在代码中工作有任何想法,我将不胜感激。

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

只是做了一个重建,最重要的是,我浏览了每个包含的项目,并手动完全删除了/bin 和/obj 目录。 现在,当我运行项目时,发生了一些不同的事情。 每当我的代码尝试创建新的浏览器选项卡用户控件时,我都会收到一个未处理的异常:

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:UsersRobertDocumentsVisual Studio 2012ProjectsWindows FormsHiveMind3D_protoHiveMind3D_protoBrowserForm.cs:line 35
       at HiveMind3D_proto.BrowserForm..ctor() in c:UsersRobertDocumentsVisual Studio 2012ProjectsWindows FormsHiveMind3D_protoHiveMind3D_protoBrowserForm.cs:line 24
       at HiveMind3D_proto.Program.Main() in c:UsersRobertDocumentsVisual Studio 2012ProjectsWindows FormsHiveMind3D_protoHiveMind3D_protoProgram.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项目的引用来重新添加它。 但我仍然得到那个例外。

好的,我现在正在运行。 尽管使用完整的源代码而不是 NuGet 包。 事实证明,我需要做几件事:

根据常见问题解答:

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

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

    • 利布塞夫.dll

    • ICUDTL.dat

    • 头孢夏普.dll

    • CefSharp.WinForms.dll

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

    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;
            }
        }
    }
    
请注意,最好将日志文件详细

程度设置为"详细",以便在日志文件中看到任何错误。 他们可以成为救命稻草。