简体   繁体   English

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

[英]Open Form2 from Form1 (winform c# ) using javascript …cefsharp

Need to open Form3 when an HTML button is clicked on Form1. 在Form1上单击HTML按钮时需要打开Form3。 The Form3 window shows up. Form3窗口出现。 But data doesn't load into window. 但数据不会加载到窗口中。

When the same is implemented from button _click event of Form itself (Form designer) the form loads up. 当从表单本身的按钮_click事件(表单设计器)实现相同时,表单将加载。 Please Help. 请帮忙。

    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()
            }
        }

    }

Here in the above code, the button2 is created using designer. 在上面的代码中,button2是使用designer创建的。 So when I click the button2: The Form3 opens. 所以,当我点击button2时:Form3打开。 But when a button is clicked form an html page : 但是当从html页面点击按钮时:

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

The form window opens but form data doesn't load into the window. 表单窗口打开,但表单数据不会加载到窗口中。

button2 click : Form3 on Button2 click button2单击: Button2上的Form3单击

HTML button click : Form3 on html button click 单击HTML按钮: 单击html按钮上的Form3

You probably solved your problem by now, but for anyone else experiencing this issue (as I did): 您现在可能已经解决了问题,但对于遇到此问题的其他人(正如我所做的那样):

You cannot open the form directly in the C# bound function that was called from JavaScript, because it most likely blocks the render process. 您无法直接在从JavaScript调用的C#绑定函数中打开表单,因为它很可能会阻止呈现过程。

Instead, you need to invoke the form opening code onto the UI thread. 相反,您需要在UI线程上调用表单打开代码。

Here's what I've come up with: 这是我想出的:
In the constructor method of the C# class to bound, I pass in the form that holds the CEFSharp browser so I can invoke it. 在要绑定的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();
            });
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM