using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using CefSharp.WinForms; using CefSharp; namespace TProduct.WebPage { public partial class WebBrowserCtrl : UserControl { public WebBrowserCtrl() { InitializeComponent(); } private ChromiumWebBrowser _chromiumWebBrowser;//浏览器控件 protected IWebBrowserEvents _webBrowserEvents2;//浏览器事件 protected virtual void InitialWebBrowser(string url) { if (!System.IO.File.Exists(url)) { MessageBox.Show(string.Format("{0} 文件丢失!",url)); return; } _chromiumWebBrowser = new ChromiumWebBrowser(url); _chromiumWebBrowser.Dock = DockStyle.Fill; _chromiumWebBrowser.Margin = new Padding(0); //此设置允许网页方法首字母大写 //new BindingOptions { CamelCaseJavascriptNames = false } //Old method _chromiumWebBrowser.RegisterJsObject("callbackObj", _webBrowserEvents2, null); //_chromiumWebBrowser.RegisterJsObject("callbackObj", new BoundObject(), options: BindingOptions.DefaultBinder); //Replaced with //CefSharpSettings.LegacyJavascriptBindingEnabled = true; //CefSharpSettings.WcfEnabled = true; //_chromiumWebBrowser.JavascriptObjectRepository.Register("callbackObj", _webBrowserEvents2, isAsync: false, options: BindingOptions.DefaultBinder); this.Controls.Add(_chromiumWebBrowser); } public async Task EvaluateScriptAsync(string script, TimeSpan? timeout = null) { var task = _chromiumWebBrowser.EvaluateScriptAsync(script, timeout); await task; return new TProduct.Model.WebBrowserResponse() { Message = task.Result.Message, Result = task.Result.Result, Success = task.Result.Success }; } protected async Task EvaluateScriptAsync(TimeSpan? timeout, string methodName, params object[] args) { var task = _chromiumWebBrowser.EvaluateScriptAsync(timeout, methodName, args); await task; return new TProduct.Model.WebBrowserResponse() { Message = task.Result.Message, Result = task.Result.Result, Success = task.Result.Success }; } protected async Task EvaluateScriptAsync(string methodName, params object[] args) { var task = _chromiumWebBrowser.EvaluateScriptAsync(methodName, args); await task; return new TProduct.Model.WebBrowserResponse() { Message = task.Result.Message, Result = task.Result.Result, Success = task.Result.Success }; } protected void ExecuteScriptAsync(string methodName, params object[] args) { _chromiumWebBrowser.ExecuteScriptAsync(methodName, args); } protected void ExecuteScriptAsync(string script) { _chromiumWebBrowser.ExecuteScriptAsync(script); } } }