• C# CefSharp


    一、

      CefSharp简单来说就是一款.Net编写的浏览器包,方便你在Winform和WPF中内嵌的Chrome浏览器组件。

      https://github.com/cefsharp/CefSharp

    二、使用

      1.安装,在vs中NuGet搜索CefSharp然后下载安装CefSharp.WinForms和CefSharp.Common;

      2.浏览引用 CefSharp.WinForms.dll,CefSharp.Core.dll,CefSharp.dll;

      3.将平台换为X86或者X64,Any Cpu无法编译成功;

    三、CefSharp 与 js 相互调用,代码举例

    using CefSharp;
    using CefSharp.WinForms;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace BlockDemo
    {
        public partial class FrmMain : Form
        {
            private ChromiumWebBrowser chromBrowser;
            public FrmMain()
            {
                InitializeComponent();
                InitChromium();
            }
            public void InitChromium()
            {
                string strUrl;
                string currentDir;
                currentDir = System.Environment.CurrentDirectory;
                strUrl = "file:///" + currentDir + "/html/index.html";
                strUrl = strUrl.Replace('\', '/');            
    
                CefSettings settings = new CefSettings();
                settings.Locale = "zh-CN";
                settings.CefCommandLineArgs.Add("disable-gpu", "1");//去掉gpu,否则chrome显示有问题
                Cef.Initialize(settings);          
    
                chromBrowser = new ChromiumWebBrowser(strUrl);   
    
                BrowserSettings browserSettings = new BrowserSettings();
                browserSettings.FileAccessFromFileUrls = CefState.Enabled;
                browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
                chromBrowser.BrowserSettings = browserSettings;
    
                panel1.Controls.Add(chromBrowser);
                chromBrowser.Dock = DockStyle.Fill;
    
                CefSharpSettings.LegacyJavascriptBindingEnabled = true;//新cefsharp绑定需要优先申明
                    chromBrowser.RegisterJsObject("jsObj", new JsEvent(), 
                    new CefSharp.BindingOptions() { CamelCaseJavascriptNames = false });
            }
            private void FrmMain_Load(object sender, EventArgs e)
            {
    
            }
    
            private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
            {
                chromBrowser.Dispose();
                chromBrowser = null;
                CefSharp.Cef.Shutdown();
            }
    
            private void btnEvaluateScript_Click(object sender, EventArgs e)
            {      
    
                Task<CefSharp.JavascriptResponse> t = chromBrowser.EvaluateScriptAsync("get_test(1,99)");
                // 等待js 方法执行完后,获取返回值
                t.Wait();
                // t.Result 是 CefSharp.JavascriptResponse 对象
                // t.Result.Result 是一个 object 对象,来自js的 get_test() 方法的返回值
                if (t.Result.Result != null)
                {
                    MessageBox.Show(t.Result.Result.ToString());
                }
            }
    
        }
    
        public class JsEvent
        {
            public string MessageText { get; set; }
            public void ShowTest()
            {
                MessageBox.Show("this in C#.
    
    " + MessageText);
            }       
        }
    }
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <title>Bootstrap 101 Template</title>
    
        <!-- Bootstrap -->
        <link href="bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
    
        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
          <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
          <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->
      </head>
      <body>
        <h1>Hello, world!</h1>    
        <div><button type="button" onclick="test()">Test</button></div>
        <div><label id="msg"></label></div>
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="jquery-3.4.1.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
        <script type="text/javascript">
         function test(){
          jsObj.MessageText = "我是js";
          jsObj.ShowTest();
          $("#msg").text("调用C#里函数");
         }
         function get_test(a,b){      
           $("#msg").text(a+b);
           return a+b;
         }
        </script
      </body>
    </html>
  • 相关阅读:
    Android AIDL使用详解
    学习Spring-Data-Jpa(十七)---对Web模块的支持
    学习Spring-Data-Jpa(十六)---@Version与@Lock
    学习Spring-Data-Jpa(十五)---Auditing与@MappedSuperclass
    学习Spring-Data-Jpa(十四)---自定义Repository
    学习Spring-Data-Jpa(十三)---动态查询接口JpaSpecificationExecutor
    学习Spring-Data-Jpa(十二)---投影Projections-对查询结果的扩展
    学习Spring-Data-Jpa(十一)---抓取策略与实体图
    学习Spring-Data-Jpa(十)---注解式方法查询之@Query、@Modifying与派生delete
    学习Spring-Data-Jpa(九)---注解式方法查询之@NamedQuery、@NamedNativeQuery
  • 原文地址:https://www.cnblogs.com/ike_li/p/11970922.html
Copyright © 2020-2023  润新知