在 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-resourcesdashboard.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 是使用设计器创建的。因此,当我单击按钮2时:Form3将打开。但是当从 html 页面单击按钮时:
<button onclick="winformObj.openfrm();">Open</button>
窗体窗口将打开,但窗体数据不会加载到窗口中。
按钮2 点击 :按钮上的 Form3 2 单击
HTML 按钮点击 :Form3 on html 按钮单击
您现在可能已经解决了您的问题,但对于遇到此问题的任何其他人(就像我一样(:
不能直接在从 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();
});
}
}
}