gpt4 book ai didi

cefsharp - 如何将 CefSharp 依赖项和文件移动到子目录?

转载 作者:行者123 更新时间:2023-12-02 05:17:29 32 4
gpt4 key购买 nike

CefSharp 有许多运行所需的依赖项和库。构建文件夹很困惑。如何将所需的 .dll 和 .pak 依赖项移至子文件夹?

最佳答案

首先,为了让一切变得更容易,我建议向 Visual Studios 添加一个文件夹并将所有必需的文件放入其中。如果您在资源管理器中创建此文件夹,请在解决方案资源管理器中单击解决方案上方的“显示所有文件”:

enter image description here

右键单击您想要包含的文件夹和文件,然后选择“包含在项目中”。

请务必包含所有必需的 CefSharp 文件 - more info on github
您最终应该得到一个与此类似的文件树:

enter image description here

请务必将所有文件的属性下的“复制到输出目录”更改为“始终复制”。

enter image description here

现在是代码。您的解决方案应该有一个“App.config”文件(如果没有,请谷歌一下,您会找到生成该文件的方法)。

您将向其中添加一个新的 assembleBindingprobing 元素 ( MSDN - probing )
probing 元素告诉 Windows 它应该在其他文件夹中查找库。因此,我们可以通过这种方式加载 CefSharp 所需的所有 .dll。

示例 App.config:

<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="resources/cefsharp" />
</assemblyBinding>
</runtime>
</configuration>

注意:路径是相对于 .exe 文件的位置。

现在已经处理了 .dll 文件,但我们现在需要更改 CefSharp 的设置,以便它知道在哪里查找 .pak 文件、区域设置和 BrowserSubprocess.exe。

为此,我们将定义所有文件路径并手动将它们分配给 CefSharp。

下面是它应该是什么样子的示例:

// File location variables
static string lib, browser, locales, res;

[STAThread]
static void Main()
{
// Assigning file paths to varialbles
lib = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"resources\cefsharp\libcef.dll");
browser = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"resources\cefsharp\CefSharp.BrowserSubprocess.exe");
locales = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"resources\cefsharp\locales\");
res = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"resources\cefsharp\");

var libraryLoader = new CefLibraryHandle(lib);
bool isValid = !libraryLoader.IsInvalid;
Console.WriteLine($"Library is valid: {isValid}");

LoadForm();

libraryLoader.Dispose();
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void LoadForm()
{
var settings = new CefSettings();
settings.BrowserSubprocessPath = browser;
settings.LocalesDirPath = locales;
settings.ResourcesDirPath = res;

Cef.Initialize(settings, shutdownOnProcessExit: false, performDependencyCheck: false);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new CefWinForm());
}

所有内容改编自:https://github.com/cefsharp/CefSharp/issues/601
最初的问题很难完全遵循并正常工作,所以我想我会分享这些知识,以防将来有人遇到类似的问题。

注意:Visual Studio 仍将在输出目录中包含 .dll、.pak、.xml 等,但您可以通过从主文件夹中删除依赖项(保留资源文件夹)来检查构建是否成功.

关于cefsharp - 如何将 CefSharp 依赖项和文件移动到子目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39400139/

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