当前位置:   article > 正文

C# 引用 WebBrowser、WebKitBrowser、CefSharp 数据交互的方法_cefsharp c#通信

cefsharp c#通信

在开发过程中,C#引用BS端的页面可谓 繁琐,下面就 常见的 交互方法 分享给大家

一: 最基础的 WebBrowser        

1、在窗体拖入 WebBrowser   控件后先设置COM可见,否则会报错       
 

  1. //必须要有
  2.  [System.Runtime.InteropServices.ComVisibleAttribute(true)]
  3. public partial class MyFormTest : Form
  4.            public MyFormTest ()
  5. {
  6.                 InitializeComponent();
  7.             }
  8.          public void ShowMyData(string MyParams) // 在JS中可调用测方法
  9.           {
  10.          }
  11.      }

2  设置webBrowser 的属性  

  1. private void MyFormTest_Load(object sender, EventArgs e)
  2.   {
  3.           webBrowser1.ScriptErrorsSuppressed = true;
  4.             webBrowser1.IsWebBrowserContextMenuEnabled = false;
  5.             webBrowser1.WebBrowserShortcutsEnabled = false;
  6.             webBrowser1.AllowWebBrowserDrop = false;
  7.             webBrowser1.ObjectForScripting = this;  //通过这个可以把winform中的变量传递到js中,类型必须是com可见;
  8.             string url = Application.StartupPath + @"\MainHTML.html";         
  9.             webBrowser1.Navigate(url);
  10. }

3 、webBrowser 的与C# 交互

      C# 调用JS       

  1. //CreatPttjChart 为js的 方法,无参数
  2.  webBrowser1.Document.InvokeScript("CreatPttjChart"); 
  3. // GetWarmningInfo为有参数的 JS
  4.  webBrowser1.Document.InvokeScript("GetWarmningInfo", new string[] { monJson, dataJson });
  5. //  ControlID 为html中的控件
  6. webBrowser1.Document.All["ControlID"].SetAttribute("InnerText", "要传入的数据");  

      JS 调用 C# 的方法

  window.external.ShowMyData("我的测试数据"

二: 基于open-webkit-sharp-master 的 WebKitBrowser  C#与 JS 进行交互(引用方法参照

https://blog.csdn.net/wn2utt18/article/details/85781818,不在赘述)

 1、在窗体拖入 WebKitBrowser  控件后先设置COM可见,否则会报错         

  1.  [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
  2. [ComVisible(true)]//COM+组件可见
  3. public partial class MyFormTest : Form
  4. {
  5. // 

2  设置WebKitBrowser  的属性      

  1.   private void MyFormTest_Load(object sender, EventArgs e)
  2.    {
  3.           string url = Application.StartupPath + @"\MainHTML.html";
  4.             webKitBrowser1.DocumentCompleted += new    WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);        
  5.             webKitBrowser1.Navigate(url);
  6.   }
  7.  private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  8.  {
  9.             webBrowser1.GetScriptManager.ScriptObject = this;//设置脚本对象
  10.             webBrowser1.GetScriptManager.EvaluateScript("var obj=window.external;");//设置脚本前缀兼容webbrowser的写法
  11.  }

3 、WebKitBrowser 的与C# 交互

      C# 调用JS       

  1. //CreatBJSLChart为js的 方法,无参数
  2. webKitBrowser1.GetScriptManager.CallFunction("CreatBJSLChart", new string[] { string.Empty });
  3.  // 有参数的 JS         
  4. webKitBrowser1.GetScriptManager.CallFunction("GetWarmningInfo", new string[] { monJson, dataJson }); 
  5. //设置控件的值
  6. webKitBrowser1.GetScriptManager.CallFunction("SetAttribute", new string[] { LableId, Value });  
  7.        

      JS 调用 C# 的方法

       

window.external.ShowMyData("我的测试数据"

三: 基于CefSharp 的 ChromiumWebBrowser C#与 JS 进行交互(引用方法参照

  https://blog.csdn.net/yh0503/article/details/86648682,不在赘述)

1、设置参数     

  1. //初始化浏览器并启动
  2. public void InitializeChromium()
  3.  {
  4.        CefSettings settings = new CefSettings();
  5.        // Initialize cef with the provided settings
  6.       Cef.Initialize(settings);
  7.        // Create a browser component
  8.        chromeBrowser = new ChromiumWebBrowser("https://www.baidu.com");
  9.         // Add it to the form and fill it to the form window.
  10.        this.Controls.Add(chromeBrowser);
  11.        chromeBrowser.Dock = DockStyle.Fill;
  12. //需要添加此句代码,否则下面执行会报错
  13.         CefSharpSettings.LegacyJavascriptBindingEnabled = true;                
  14.         // Register an object in javascript named "cefCustomObject" with function of
  15. // the CefCustomObject class :3
  16.      chromeBrowser.RegisterJsObject("cefCustomObject", new CefCustomObject(chromeBrowser, this)); //过时的话。用下面的
  17. chromeBrowser.JavascriptObjectRepository.Register("cefCustomObject", new
  18. CefCustomObject(chromeBrowser, this), isAsync: false, options:
  19. BindingOptions.DefaultBinder);
  20. }
  21.         
  22.  //窗体关闭时,记得停止浏览器
  23.  private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  24.  {
  25.     Cef.Shutdown();
  26. }    

2、   ChromiumWebBrowser C#与 JS 进行交互

  1. CWebBrowser.FrameLoadEnd += new EventHandler<FrameLoadEndEventArgs>(FrameEndFunc);//注册事件
  2. private void FrameEndFunc(object sender, FrameLoadEndEventArgs e)
  3. {
  4.      //EvaluateScriptAsync为调用JS的方法
  5.     string Mydata = “MyParams”; //我的参数值
  6.     string JSFunction = "initControValue('" + Mydata + "')"; //我的JS方法是 JSFunction
  7.     this.CWebBrowser.EvaluateScriptAsync(JSFunction );
  8.     // 设置值 
  9. this.CWebBrowser.EvaluateScriptAsync("document.getElementById('testid2').value='123'");
  10. }

 

以上就是三种控件的 在C# 中的交互方法,如有不当之处,请多多指正......

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/257809
推荐阅读
相关标签
  

闽ICP备14008679号