Пример #1
0
 public void RemoveBrowser(IWebBrowser browser)
 {
     lock (webBrowserDicLock_) {
         if (WebBrowserInfoDic.ContainsKey(browser))
         {
             //var info = WebBrowserInfoDic[browser];
             WebBrowserInfoDic.Remove(browser);
         }
     }
 }
Пример #2
0
        public long AddBrowser(IWebBrowser browser, Func <object> getParentControl)
        {
            if (browser == null)
            {
                throw new ArgumentException("浏览器对象不能为空", "browser");
            }
            browser.JsQuery += (webBrowser, args) =>
            {
                switch ((TneQueryId)args.CustomMsg)
                {
                case TneQueryId.RegisterNativeMap:
                    OnRegisterNativeMap(webBrowser as IWebBrowser, args);
                    break;

                case TneQueryId.NativeMap:
                    OnJavaScriptNativeMap(webBrowser as IWebBrowser, args);
                    break;

                case TneQueryId.DeleteNativeObject:
                    OnDeleteNativeObject(webBrowser as IWebBrowser, args);
                    break;

                case TneQueryId.GetThisFormHashCode:
                {
                    var wb     = webBrowser as IWebBrowser;
                    var wbinfo = GetBrowserInfo(wb);
                    wb.ResponseJsQuery(args.WebView, args.QueryId, args.CustomMsg, wbinfo.ParentControlId.ToString());
                }
                break;

                case TneQueryId.RunFunctionForTneForm:
                {
                    var runInfo = JsonConvert.DeserializeObject <RunFunctionForTneFormInfo>(args.Request);
                    var wb      = webBrowser as IWebBrowser;
                    var wbinfo  = GetBrowserInfo(wb);
                    var tneForm = wbinfo.GetNativeObject(runInfo.TneFormId, false) as TneForm;
                    var result  = tneForm.WebBrowser.RunJs($"return (async {runInfo.Function})(\"{runInfo.Arg}\").then(async function(result){{await Tnelab.TneQueryAsync(Tnelab.TneQueryId.RunFunctionResultForTneForm, result);}})");
                    if (taskRunFunctionResult_ != null)
                    {
                        throw new Exception("runfunction异常");
                    }
                    taskRunFunctionResult_ = new TaskCompletionSource <string>();
                    Task.Factory.StartNew(() => {
                            taskRunFunctionResult_.Task.Wait();
                            wb.UIInvoke(() => {
                                wb.ResponseJsQuery(args.WebView, args.QueryId, args.CustomMsg, taskRunFunctionResult_.Task.Result);
                            });
                            taskRunFunctionResult_ = null;
                        });
                }
                break;

                case TneQueryId.RunFunctionResultForTneForm:
                {
                    var wb = webBrowser as IWebBrowser;
                    wb.ResponseJsQuery(args.WebView, args.QueryId, args.CustomMsg, "OK");
                    taskRunFunctionResult_.SetResult(args.Request);
                }
                break;

                case TneQueryId.ShowContextMenuForTneForm:
                {
                    OnShowContextMenu(webBrowser as IWebBrowser, args);
                }
                break;

                default:
                    throw new Exception("未知JS消息");
                }
            };
            var info = new WebBrowserInfo(browser, getParentControl);

            lock (webBrowserDicLock_)
            {
                WebBrowserInfoDic.Add(browser, info);
            }
            return(info.ParentControlId);
        }