无法使用 CefSharp 嵌入式 Chromium 浏览器控件加载网页
Unable to load web page using the CefSharp embedded Chromium browser control?
艰难。 首先,我尝试使用 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; } } }
程度设置为"详细",以便在日志文件中看到任何错误。 他们可以成为救命稻草。
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- WinAPI 在单击第一个对话框上的按钮控件并销毁第一个对话框后创建第二个对话框
- 在编译时,C++项目抛出错误 C2228,这是预期的,因为控件在运行时未达到该点
- 如何更改窗体上所有控件的标题?[C++生成器]
- 双击更改 mfc 中列表控件中的行的颜色
- 派生的 wxPanel 控件如何访问其中包含 wxDialog 中的数据?
- 如何从代码本身向 wxwidgets 中的文本控件插入字符?
- 如何在MFC中的静态文本控件上插入图标?
- 我的主窗口在创建时或单击更新区域时是否会收到编辑控件?
- 如何在Qt C++中向自定义控件添加属性?
- C/C++ 检测双击 TVItem 的常用控件
- 如何隐藏显示HTML5标记的Web浏览器控件(IE)的滚动条和窗口边框
- 重用 Web 浏览器控件
- Web浏览器控件仿真问题(FEATURE_browser_emulation)
- 如何在 webkit 浏览器控件中返回指向 JavaScript 的 IDispatch 指针
- 无法使用 CefSharp 嵌入式 Chromium 浏览器控件加载网页
- MFC Web 浏览器控件:模拟 Ctrl+N 需要多少(正常)代码行
- Web浏览器控件中的c++:检测鼠标点击特定按钮
- 在MFC应用程序的IE web浏览器控件中运行userscript
- 如何在不混淆的情况下监视.NET web浏览器控件上的用户操作