2016-09-08 138 views

回答

5

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

enter image description here

这右击该文件夹(S)和文件(县)要包括和选择'包含在项目中'。

一定要包括所有需要的CefSharp文件 - more info on github
你应该用一个文件树,看起来类似于这样结束了:

enter image description here

一定要改变“复制到输出Directy”来在所有文件的属性下“始终复制”。

enter image description here

现在的代码。你的解决方案应该有一个'App.config'文件(如果没有,谷歌周围,你会找到方法来产生一个)。

你要到一个新的assemblyBindingprobing元素添加进去(MSDN - probing
probing元素告诉它应该在其他文件夹图书馆的窗户。因此,我们可以通过这种方式加载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等,但是您可以通过从主文件夹中删除依赖关系来检查构建是否成功(将资源文件夹)。

+1

非常有帮助的教程,谢谢。 然而,你错过了'CefLibraryHandle'的定义,你(需要它的人)可以在这里找到它: https:// github。COM/cefsharp/CefSharp /斑点/主/ CefSharp/CefLibraryHandle.cs – Jhollman

相关问题