VSTO Addin中的CefSharp [英] CefSharp in VSTO Addin
问题描述
我正在尝试在VSTO Excel插件中使用CefSharp WindowForm控件。 CefSharp.WinForms版本为75.1.142,我正在通过VS 2017在Excel 2013(64位)上添加插件。
I'm trying to use CefSharp WindowForm Control in the VSTO Excel Addin. The CefSharp.WinForms version is 75.1.142 and I'm making an addin on Excel 2013 (64 Bit) through VS 2017.
我收到FileNotFoundException:'无法加载文件或程序集 CefSharp,版本= 75.1.142.0,文化=中性,PublicKeyToken = 40c4b6fc221f4138或其依赖项之一。系统找不到指定的文件。'
在下面的代码执行中。
I'm getting FileNotFoundException: 'Could not load file or assembly 'CefSharp, Version=75.1.142.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138' or one of its dependencies. The system cannot find the file specified.' on the below code execution.
public void InitBrowser()
{
var cefSettings = new CefSettings();
cefSettings.MultiThreadedMessageLoop = true;
// I'm setting current directory to D:\\CEF\\cefsharp\\ExcelWinform\\ExcelWinformAddIn\\bin\\x64\\Debug\\ where all the CefSharp dlls and dependencies are present.
cefSettings.BrowserSubprocessPath = "D:\\CEF\\cefsharp\\ExcelWinform\\ExcelWinformAddIn\\bin\\x64\\Debug\\CefSharp.BrowserSubprocess.exe";
if (!Cef.Initialize(cefSettings, performDependencyCheck: true, browserProcessHandler: null))
{
throw new Exception("Unable to Initialize Cef");
}
browser = new ChromiumWebBrowser("http://www.google.com");
{
Dock = DockStyle.Fill;
}
BrowserSettings browserSettings = new BrowserSettings();
//browserSettings.FileAccessFromFileUrls = CefState.Enabled;
//browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
browserSettings.BackgroundColor = 0xFFFF00;
browserSettings.LocalStorage = CefState.Enabled;// enable Local access
browser.BrowserSettings = browserSettings;
this.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
If instead of VSTO Excel addin, I just create a simple WindowFrom application then the abve code is working fine.
推荐答案
使用 CefSharp $不支持在
Office VSTO
插件/插件中直接使用c 。
Using CefSharp
directly in an Office VSTO
plugin/addin is not supported.
如果为一个应用程序安装了多个VSTO加载项,则每个VSTO加载项将加载到不同的应用程序域中。
If multiple VSTO Add-ins are installed for an application, each VSTO Add-in is loaded in a different application domain.
根据 https://docs.microsoft.com/zh-cn/visualstudio/vsto/architecture-of-vsto-add-ins?view=vs-2019 Office VSTO
将使用 AppDomain
加载您的加载项
(我相信即使您仅加载了
As per https://docs.microsoft.com/en-us/visualstudio/vsto/architecture-of-vsto-add-ins?view=vs-2019 Office VSTO
will load your add-in
using as AppDomain
(I believe even if you only load a single add-in the non-default appdomain will be used).
CefSharp
是使用<$ c $构建的。 c> VC ++ 不能在非默认 AppDomain
中加载。您看到的错误与插件无法加载 libcef.dll
有关,您可以解决此问题,仅面对无法通过跨AppDomains的GCHandle
。
CefSharp
is built using VC++
which cannot be loaded in the non-default AppDomain
. The error you are seeing is related to the plugin being unable to load libcef.dll
, you could resolve this issue, only to be faced with Cannot pass a GCHandle across AppDomains
.
您可以在进程外运行 CefSharp
有一个例子。这有点过时了,基本原理仍然应该是相关的。参见 https://github.com/stever/AppHostCefSharp
You can run CefSharp
out of process, for which there is an example. It's a little outdated, the fundamentals should still be relevant. See https://github.com/stever/AppHostCefSharp
我个人的建议是使用 CefGlue 或 ChromiumFx 。两者都是基于 P / Invoke
的,因此请使用根本不同的方法与不受管理的 libcef.dll
进行交互。
My personal recommendation is to use CefGlue or ChromiumFx. Both are P/Invoke
based so use a fundamentally different approach to interacting with the unmanaged libcef.dll
.
另请参见
- https://github.com/cefsharp/CefSharp/issues/1191
- https://github.com/cefsharp/CefSharp/issues/351
- ExcelDNA中的Cefsharp
- >无法跨AppDomains传递GCHandle:无解决方案代表?
- https:// dev elopercommunity.visualstudio.com/content/problem/169359/ccli-dll-is-initialized-in-wrong-appdomain.html
- https://github.com/cefsharp/CefSharp/issues/1191
- https://github.com/cefsharp/CefSharp/issues/351
- Cefsharp in ExcelDNA
- Cannot pass a GCHandle across AppDomains: solution without delegates?
- https://developercommunity.visualstudio.com/content/problem/169359/ccli-dll-is-initialized-in-wrong-appdomain.html
这篇关于VSTO Addin中的CefSharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!