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

时间:2015-01-17 21:06:53

标签: c# c++ chromium cefsharp nullptr

我有一个粗略的开始。首先,我尝试使用NuGet包。当我安装它们时, CefSharp DLL中没有必要的引用出现在我的References列表中。所以我通过搜索 / 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 repo解决方案的副本中跟踪这个完全相同的代码, 进入“if”块并执行< em> LoadURL()方法。我检查了cefFrame的值,它与我在我的应用程序中的值相同。出于一些真正奇怪的原因,这里或我的应用程序中存在更加隐蔽的问题,或者它确实将 cefFrame {CefRefPtr} 的值与相同nullptr的不应该。

如果有人知道如何让我的代码工作正常,我会非常感激。

更新:我进行了三重检查,并且构建配置在执行工作的GitHub CefSharp解决方案与不支持的项目之间是相同的。除了设置为Win32的Core项目(在两种情况下)外,一切都设置为x86。

我刚刚进行了重建,最重要的是我完成了每个包含的项目,并完全手动删除了/ 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项目的引用来重新添加它。但我仍然得到那个例外。

1 个答案:

答案 0 :(得分:0)

好的,我现在正在跑步。尽管有完整的源代替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;
            }
        }
    }
    

注意,将日志文件详细程度设置为详细信息是个好主意,这样您就可以在日志文件中看到任何错误。他们可以成为救星。