[英]CefSharp how to clear current chromium web browser control content like real browser C#
[英]Unable to load web page using the CefSharp embedded Chromium browser control?
我正在粗略地开始它。 首先,我尝试使用NuGet软件包。 当我安装它们时,没有任何对CefSharp DLL的必要引用显示在“引用”列表中。 因此,我通过搜索/ packages子目录来手工添加它们。 在构建完项目之后,我发现执行URL加载时没有任何反应 。 没有加载网页,没有错误消息,没有触发异常。 没有。
我完全删除了CefSharp Nuget软件包,并从GitHub提取了最新的回购源。 首先,我要确保CefSharp.WinForms.Example项目中的URL加载正常。 效果很好。 然后,我将必要的项目集从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>}
但是,它显然不是NULL,但好像已经被处理了。 (或者我对{CefRefPtr}值的分析是错误的,我的分析也是错误的。)
在任何一种情况下,“ if”语句都将cefFrame的当前值视为等于nullptr,并跳过在cefFrame引用的对象上调用LoadURL()方法的行 。 我在GitHub CefSharp存储库解决方案的副本中跟踪了完全相同的代码,并且确实输入了“ if”块并执行LoadURL()方法。 我检查了cefFrame的值,它与我的应用程序中的值相同。 由于某些真正奇怪的原因,这里或我的应用程序中存在一个更隐患的问题,或者它确实确实应将cefFrame的{CefRefPtr}的值与nullptr的值相同。
如果有人对如何在代码中正常工作有任何想法,我将非常感谢。
更新:我仔细检查了一下,在可行的GitHub CefSharp解决方案与无效的GitHub CefSharp解决方案之间,构建配置相同。 除了将Core项目设置为Win32(在两种情况下)之外,所有设置都设置为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项目的引用来重新添加它。 但是我仍然得到那个异常。
好的,我现在正在跑步。 尽管提供了完整的源代码,而不是NuGet软件包。 我发现我需要做一些事情:
按照常见问题解答:
https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#Runtime_dependencies
将以下文件复制到/ 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; } } }
请注意,将日志文件的详细程度设置为“详细”是个好主意,这样您就可以在日志文件中看到任何错误。 他们可以成为救生员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.