MAinForm
public MainForm() { InitializeComponent(); _thisJsObject._Main_form = this; _invokingJsObject.GetInstance().LoadMap(); webBrowserPanel.Controls.Add(_invokingJsObject.GetInstance().WebBrowser); }
_invokingJsObject
class _invokingJsObject { public string load_path = "http://127.0.0.1:8088/map/index.html"; private static _invokingJsObject instance = null; private _thisJsObject _obj = new _thisJsObject(); public ChromiumWebBrowser WebBrowser { get; set; } public static _invokingJsObject GetInstance() { if (instance == null) { instance = new _invokingJsObject(); instance.WebBrowser = new ChromiumWebBrowser(instance.load_path); } return instance; } public _invokingJsObject() { load_path = System.Environment.CurrentDirectory; load_path += "\\CallUrl\\webrtc.html"; CefSharpSettings.LegacyJavascriptBindingEnabled = false; CefSettings setting = new CefSettings(); setting.Locale = "zh-CN"; setting.CefCommandLineArgs.Add("enable-media-stream", "1"); //开启摄像头 setting.CefCommandLineArgs.Add("enable-system-flash", "1"); //flash setting.CefCommandLineArgs.Add("enable-speech-input", "1"); //语音输入 Cef.Initialize(setting); } /// <summary> /// 加载地图 /// </summary> public void LoadMap() { WebBrowser.Dock = DockStyle.Fill; BrowserSettings browserSettings = new BrowserSettings(); browserSettings.FileAccessFromFileUrls = CefState.Enabled; browserSettings.UniversalAccessFromFileUrls = CefState.Enabled; browserSettings.WebSecurity = CefState.Enabled; WebBrowser.BrowserSettings = browserSettings; WebBrowser.JavascriptObjectRepository.ResolveObject += (s, eve) => { var repo = eve.ObjectRepository; if (eve.ObjectName == "TestJsObject") { repo.Register("TestJsObject", _obj, isAsync: true, options: BindingOptions.DefaultBinder); } }; WebBrowser.JavascriptObjectRepository.Register("TestJsObject", _obj, isAsync: true, options: BindingOptions.DefaultBinder); WebBrowser.FrameLoadEnd += new EventHandler<FrameLoadEndEventArgs>(FrameEndFunc); //WebBrowser.Load(load_path); } //地图加载完毕事件 private void FrameEndFunc(object sender, FrameLoadEndEventArgs e) { //地图加载完毕回调事件 var browser = WebBrowser.GetBrowser(); browser.ShowDevTools(); init("60000009", "sip123", "exp.longbaotec.cn"); } //初始化js public void init(String SipUser, String SipPwd, String SipIp) { String script = string.Format("init(‘{0}‘,‘{1}‘,‘{2}‘)", SipUser, SipPwd, SipIp); script = script.Replace("\r\n", ""); this.WebBrowser.EvaluateScriptAsync(script); } }
_thisJsObject
public class _thisJsObject { public static WebRtcCSharp.MainForm _Main_form { get; set; } public void GetWebRtcInit() { //_Main_form.OnInit(); } public void simpleCallBack(String msg,String msg2) { } }
js 中
//C#调用js文件
function init(me,pws,domain) { alert(123); }
//js调用C#代码
function callback(msg,msg2) { CefSharp.BindObjectAsync(‘TestJsObject‘).then(function (result) { TestJsObject.simpleCallBack(msg,msg2); }); }
cefSharp 版本为
博客记录自己写代码碰到的坑:
1、C# cef最新版本 js与C#调用与老版本相比改动较大;
2、打开本地监控画面与麦克风;
所以文字描述简单;