使用javascript ... cefsharp从Form1(winform c#)打开Form2

时间:2017-05-27 05:57:09

标签: javascript c# visual-studio cefsharp

在Form1上单击HTML按钮时需要打开Form3。 Form3窗口出现。但数据不会加载到窗口中。

当从按钮实现相同时 表单加载_click表单本身(表单设计器)的事件。请帮助。

    using System; 
    using System.Diagnostics;
    using System.Windows.Forms;
    using System.Data.SQLite;
    using CefSharp;
    using CefSharp.WinForms;
    using System.IO;
    using Newtonsoft.Json;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using System.Text;
    using System.Linq;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                InitializeChromium();
                chromeBrowser.RegisterJsObject("winformObj", new JavaScriptInteractionObj());
                this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

            }

            public ChromiumWebBrowser chromeBrowser;

            public void InitializeChromium()
            {

                CefSettings settings = new CefSettings();

                String page = string.Format(@"{0}\html-resources\dashboard.html", Application.StartupPath);

                if (!File.Exists(page))
                {
                    MessageBox.Show("Error The html file doesn't exists : " + page);
                }
                Cef.Initialize(settings);
                chromeBrowser = new ChromiumWebBrowser(page);
                chromeBrowser.MenuHandler = new CustomMenuHandler();
                this.Controls.Add(chromeBrowser);
                chromeBrowser.Dock = DockStyle.Fill;

                BrowserSettings browserSettings = new BrowserSettings();
                browserSettings.FileAccessFromFileUrls = CefState.Enabled;
                browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
                chromeBrowser.BrowserSettings = browserSettings;
            }


            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                Cef.Shutdown();
            }

            public void openup()
            {
                if (Application.OpenForms.OfType<Form3>().Count() == 1) Application.OpenForms.OfType<Form3>().First().Close();
                Form3 frm = new Form3();
                frm.ShowDialog();
                frm.MinimizeBox = false;
                GraphicsPath path = new GraphicsPath();
                Rectangle pathRect = new Rectangle(0, 0, 2000, 2000);
                path.AddRectangle(pathRect);
                Region region = new Region(path);
                frm.Region = region;
            }
            public void button2_Click(object sender, EventArgs e)
            {
                if (Application.OpenForms.OfType<Form3>().Count() == 1) Application.OpenForms.OfType<Form3>().First().Close();
                Form3 frm = new Form3();
                frm.Show();
                frm.MinimizeBox = false;
                GraphicsPath path = new GraphicsPath();
                Rectangle pathRect = new Rectangle(0, 0, 2000, 2000);
                path.AddRectangle(pathRect);
                Region region = new Region(path);
                frm.Region = region;
            }

            private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                Application.Exit();
            }
        }

        class CefCustomObject
        {
            // Declare a local instance of chromium and the main form in order to execute things from here in the main thread
            private static ChromiumWebBrowser _instanceBrowser = null;
            // The form class needs to be changed according to yours
            private static Form1 _instanceMainForm = null;


            public CefCustomObject(ChromiumWebBrowser originalBrowser, Form1 mainForm)
            {
                _instanceBrowser = originalBrowser;
                _instanceMainForm = mainForm;
            }

            public void showDevTools()
            {
                _instanceBrowser.ShowDevTools();
            }

            public void opencmd()
            {
                ProcessStartInfo start = new ProcessStartInfo("cmd.exe", "/c pause");
                Process.Start(start);
            }
        }



        public class JavaScriptInteractionObj
        {
            public void openfrm()
            {
                Application.OpenForms.OfType<Form1>().First().openup()
            }
        }

    }

在上面的代码中,button2是使用designer创建的。所以,当我点击button2时:Form3打开。 但是当从html页面点击按钮时:

    <button onclick="winformObj.openfrm();">Open</button>

表单窗口打开,但表单数据不会加载到窗口中。

按钮2点击: Form3 on Button2 click

HTML按钮点击: Form3 on html button click

1 个答案:

答案 0 :(得分:0)

您现在可能已经解决了问题,但对于遇到此问题的其他人(像我一样):

您不能直接在从JavaScript调用的C#绑定函数中打开表单,因为它很可能会阻止呈现过程。

相反,您需要在UI线程上调用表单打开代码。

这是我想出的内容:
在要绑定的C#类的构造方法中,我传递了包含CEFSharp浏览器的表单,以便可以调用它。

// using statements
// ...
namespace SomeProgram
{
    public class BoundClass
    {
        // Pass in a reference of the browser's form
        Form form;
        public BoundClass(Form formRef)
        {
            form = formRef;
        }

        // Open Form3 here
        public void OpenForm()
        {
            form.Invoke((MethodInvoker)delegate
            {
                Form3 theForm = new Form3();
                theForm.Show();
            });
        }

    }
}