简体   繁体   中英

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

Need to open Form3 when an HTML button is clicked on Form1. The Form3 window shows up. 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. 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. So when I click the button2: The Form3 opens. But when a button is clicked form an html page :

    <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

HTML button click : Form3 on html button click

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.

Instead, you need to invoke the form opening code onto the UI thread.

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.

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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