• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# cef 与js 交互 并且打开摄像头与麦克风

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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、打开本地监控画面与麦克风;

所以文字描述简单;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap