1. 创建.net framework form类型的项目
  1. 虽然网络说wpf类型项目是未来,form将淘汰,但是我不喜欢用xml生成界面,可视化设计+程序生成更加容易使用
  2. 使用.NET framework(不要使用.NET core,不然在Register JS时提示BrowserSubprocess错误),据说可以跨多个平台,暂时没有验证

 

 

【备忘】CefSharp使用

  1. 添加CefWinForms

使用NuGet工具安装CefSharp.WinForms,它依赖CefSharp.Common,CefSharp.Common又依赖两个cef.redist,libcef.dll都在两个cef.redist中

 

【备忘】CefSharp使用

  1. 修改工程文件
  1. 将以下内容添加到.csproj文件尾部,否则,怎么都编译不过

<ItemGroup>

<!-- TODO: These updates are currently required because CefSharp.WinForms specifies

 <Private>false</Private>, which means these libraries will not be specified in

 the .deps.json file, and so the CoreCLR wouldn't load these. -->

<Reference Update="CefSharp">

  <Private>true</Private>

</Reference>

<Reference Update="CefSharp.Core">

  <Private>true</Private>

</Reference>

<Reference Update="CefSharp.WinForms">

  <Private>true</Private>

</Reference>

</ItemGroup>

 

<!-- Include CefSharp.BrowserSubprocess.Core so we can selfhost the BrowserSubProcess using our exe -->

<Choose>

<When Condition="'$(PlatformTarget)' == 'x64'">

  <ItemGroup>

<Reference Include="CefSharp.BrowserSubprocess.Core">

  <HintPath>$(CefSharpBrowserProcessCore64)</HintPath>

  <Private>true</Private>

</Reference>

  </ItemGroup>

</When>

<!-- x86, Win32 and AnyCPU -->

<Otherwise>

  <ItemGroup>

<Reference Include="CefSharp.BrowserSubprocess.Core">

  <HintPath>$(CefSharpBrowserProcessCore32)</HintPath>

  <Private>true</Private>

</Reference>

  </ItemGroup>

</Otherwise>

</Choose>

 

  1. 修改编译配置

在删除AnyCPU,添加x64\x86;

或者使用AnyCPU,在.csproj工程文件的第一个<PropertyGroup>中增加一行

<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>

 

【备忘】CefSharp使用

  1. 修改程序
  1. Programe.cs中Main改成如下形式

public static int Main(string[] args)

{

Application.SetHighDpiMode(HighDpiMode.SystemAware);

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

 

//For Windows 7 and above, best to include relevant app.manifest entries as well

Cef.EnableHighDPISupport();

 

#if NETCOREAPP

//We are using our current exe as the BrowserSubProcess

//Multiple instances will be spawned to handle all the

//Chromium proceses, render, gpu, network, plugin, etc.

var subProcessExe = new CefSharp.BrowserSubprocess.BrowserSubprocessExecutable();

var result = subProcessExe.Main(args);

if (result > 0)

{

return result;

}

#endif

 

var settings = new CefSettings()

{

//By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data

CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache")

};

 

#if NETCOREAPP

//We use our Applications exe as the BrowserSubProcess, multiple copies

//will be spawned

var exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

settings.BrowserSubprocessPath = exePath;

#endif

 

//Example of setting a command line argument

//Enables WebRTC

settings.CefCommandLineArgs.Add("enable-media-stream");

 

//Perform dependency check to make sure all relevant resources are in our output directory.

Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);

 

var browser = new BrowserForm();

Application.Run(browser);

return 0;

  1. 程序中使用CefChromium

  string path = "www.baidu.com";

  webBrower = new ChromiumWebBrowser(path) { Dock = DockStyle.Fill };//填充方式

  this.Controls.Add(webBrower);

也可以调用Load函数加载服务端url或者本地URL

 

  1. 向js注册本地函数

CefSharpSettings.LegacyJavascriptBindingEnabled = true;

CefSharpSettings.WcfEnabled = true;

string home = Util.pathToUrl(Util.addPath(workDir, "env/home.html"));

browser = new ChromiumWebBrowser(home);

browser.Dock = DockStyle.Fill;

this.toolStripContainer.ContentPanel.Controls.Add(browser);

var bindingOptions = new BindingOptions() { CamelCaseJavascriptNames = false };

var repo = browser.JavascriptObjectRepository;

 

repo.Register("X", new JsActor(), false, options: bindingOptions);

相关文章: