Esempio n. 1
0
        private static void LoadApp()
        {
            // Make sure you set performDependencyCheck false
            if (!ChromiumWebBrowserManager.InitializeCef(false, null, settings =>
            {
                // Set BrowserSubProcessPath based on app bitness at runtime
                settings.BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
                                                              Environment.Is64BitProcess ? "x64" : "x86",
                                                              "CefSharp.BrowserSubprocess.exe");
            }))
            {
                MessageBox.Show("CEF³õʼ»¯Ê§°Ü£¡");
                return;
            }

            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
Esempio n. 2
0
        public MainForm()
        {
            InitializeComponent();

            this.FormClosing += MainForm_FormClosing;

            _webBrowser = ChromiumWebBrowserManager.InitializeChromium(panel1, options =>
            {
                options.Url = $"file:///{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Test.html") }";
                //options.Url = "http://sean.tools.com/";
                //options.DisableContextMenu = true;
                options.DownloadHandler = new DefaultDownLoadHandler
                {
                    OnDownloadComplete = (chromiumWebBrowser, downloadItem) =>
                    {
                        // 跨线程操作
                        Invoke(new Action(() =>
                        {
                            MessageBox.Show("下载完成!", "提示");
                        }));
                    }
                };
            }, browser =>
            {
                #region How do you expose a .NET class to JavaScript?
                // https://github.com/cefsharp/CefSharp/issues/2990
                // https://github.com/cefsharp/CefSharp/wiki/General-Usage#3-how-do-you-expose-a-net-class-to-javascript

                // 只需要全局设置1次即可,建议放到初始化CEF的时候调用
                CefSharpSettings.LegacyJavascriptBindingEnabled = true;
                //CefSharpSettings.WcfEnabled = true;// 当isAsync=false的时候才需要设置

                //browser.RegisterJsObject(nameof(JsEvent), new JsEvent(), new BindingOptions { CamelCaseJavascriptNames = false });// 这是cefsharp老版本支持的写法,最新版本已不支持(运行时会抛出异常)

                browser.JavascriptObjectRepository.Register(nameof(JsEvent), new JsEvent(), isAsync: true, options: new BindingOptions {
                    CamelCaseJavascriptNames = false
                });
                #endregion
            });
        }
Esempio n. 3
0
 private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
 {
     ChromiumWebBrowserManager.ShutdownCef();
 }