这篇文章主要写给前端程序员的cefsharp开发入门教程.

    1. 环境要求
      1. 使用带Windows操作系统的电脑,并安装Visual Studio开发工具.
      2. CefSharp的CefSharp Github地址
      3. 最最最主要的参考文档链接General-Usage!!!
    2. 开始搭建开发环境
      1. 先新建一个winform程序

    image.png
    然后项目初始后,界面如下
    image.png
    在右侧引用,引入CefSharp
    image.png
    添加成功后会在左侧工具栏看到相关控件
    image.png
    拖拽添加到主界面
    image.png
    到这一步,开发环境搭建完毕.

    客户端核心代码如下

    1. public partial class Form1 : Form
    2. {
    3. public Form1()
    4. {
    5. InitializeComponent();
    6. CefSettings settings = new CefSettings();
    7. settings.Locale = "zh-CN";
    8. Cef.EnableHighDPISupport();
    9. CefSharpSettings.WcfEnabled = true;
    10. // Initialize cef with the provided settings
    11. Cef.Initialize(settings, true, browserProcessHandler: null);
    12. this.chromiumWebBrowser1.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
    13. this.chromiumWebBrowser1.JavascriptObjectRepository.Register("JSBridge", new JSBridge(), isAsync: false, options: BindingOptions.DefaultBinder);
    14. }
    15. public class JSBridge
    16. {
    17. public string name = "";
    18. public void showTest(string msg)
    19. {
    20. name = msg;
    21. MessageBox.Show(name);
    22. }
    23. }
    24. private void button2_Click(object sender, EventArgs e)
    25. {
    26. this.chromiumWebBrowser1.ShowDevTools();
    27. }
    28. private void button1_Click(object sender, EventArgs e)
    29. {
    30. String url = this.textBox1.Text;
    31. try
    32. {
    33. if(url == "")
    34. {
    35. this.chromiumWebBrowser1.Load(Application.StartupPath +@"\web\index.html");
    36. }
    37. else
    38. {
    39. this.chromiumWebBrowser1.Load(url);
    40. }
    41. }
    42. catch (Exception)
    43. {
    44. throw;
    45. }
    46. }
    47. private void button3_Click(object sender, EventArgs e)
    48. {
    49. try
    50. {
    51. string str = this.textBox2.Text;
    52. this.chromiumWebBrowser1.ExecuteScriptAsync("callByNative", str);
    53. }
    54. catch (Exception ex)
    55. {
    56. MessageBox.Show(ex.Message);
    57. }
    58. }
    59. }

    前端核心代码

    <button onclick="JSBridge.showTest(1234)">call native</button>
    <script>
      function callByNative(str) {
        alert(str)
      }
    </script>
    

    最后实现界面
    image.png
    最后点击生成,可在项目同名目录下\bin\Debug文件夹内生成相关.exe文件,到此就可以把exe在别的电脑上运行了.

    完结撒花
    具体原理以后有时间再研究~~~