gpt4 book ai didi

c# - VSTO 插件中的 CefSharp

转载 作者:行者123 更新时间:2023-12-05 00:47:08 27 4
gpt4 key购买 nike

我正在尝试在 VSTO Excel 插件中使用 CefSharp WindowForm 控件。 CefSharp.WinForms 版本是 75.1.142,我正在 Excel 2013(64 位)到 VS 2017 上制作插件。

我收到 FileNotFoundException: 'Could not load file or assembly 'CefSharp, Version=75.1.142.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138' 或其依赖项之一。该系统找不到指定的文件。' 在下面的代码执行上。

    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.

最佳答案

不支持在 Office VSTO 插件/插件中使用 CefSharp 直接

If multiple VSTO Add-ins are installed for an application, each VSTO Add-in is loaded in a different application domain.

根据 https://learn.microsoft.com/en-us/visualstudio/vsto/architecture-of-vsto-add-ins?view=vs-2019 Office VSTO 将使用 AppDomain 加载您的 add-in (我相信即使您只加载一个非默认应用程序域的加载项将被使用)。

CefSharp 是使用 VC++ 构建的,无法在非默认 AppDomain 中加载。您看到的错误与插件无法加载 libcef.dll 有关,您可以解决此问题,但遇到 Cannot pass a GCHandle across AppDomains

您可以在进程外运行 CefSharp,有一个示例。它有点过时了,基本面应该仍然相关。见 https://github.com/stever/AppHostCefSharp

我个人的建议是使用 CefGlue它是基于 P/Invoke 的,因此使用完全不同的方法与非托管 libcef.dll 进行交互。

有关与 CefSharp 兼容的 VSTO 替代方案,请查看 NetOffice


另见

关于c# - VSTO 插件中的 CefSharp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58024079/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com