使用javascript…从Form1(winform c#)打开Form2
当在Form1上单击HTML按钮时,需要打开Form3。此时将显示Form3窗口。但数据不会加载到窗口中 当从按钮执行相同操作时 _单击窗体本身的事件(窗体设计器),窗体将加载。请帮忙使用javascript…从Form1(winform c#)打开Form2,javascript,c#,visual-studio,cefsharp,Javascript,C#,Visual Studio,Cefsharp,当在Form1上单击HTML按钮时,需要打开Form3。此时将显示Form3窗口。但数据不会加载到窗口中 当从按钮执行相同操作时 _单击窗体本身的事件(窗体设计器),窗体将加载。请帮忙 using System; using System.Diagnostics; using System.Windows.Forms; using System.Data.SQLite; using CefSharp; using CefSharp.WinForm
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()
}
}
}
使用系统;
使用系统诊断;
使用System.Windows.Forms;
使用System.Data.SQLite;
使用头孢沙普;
使用CefSharp.WinForms;
使用System.IO;
使用Newtonsoft.Json;
使用System.Collections.Generic;
使用System.Threading.Tasks;
使用系统文本;
使用System.Linq;
使用系统图;
使用System.Drawing.Drawing2D;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
初始化铬();
RegisterJsObject(“winformObj”,新JavaScriptInteractionObj());
this.WindowState=System.Windows.Forms.FormWindowState.Maximized;
}
公共镀铬浏览器;
公共无效初始值铬()
{
CefSettings=新的CefSettings();
String page=String.Format(@“{0}\html resources\dashboard.html”,Application.StartupPath);
如果(!File.Exists(第页))
{
Show(“错误:html文件不存在:“+page”);
}
初始化(设置);
chromeBrowser=新的ChromiumWebBrowser(第页);
chromeBrowser.MenuHandler=新的CustomMenuHandler();
this.Controls.Add(chromeBrowser);
chromeBrowser.Dock=DockStyle.Fill;
BrowserSettings BrowserSettings=新建BrowserSettings();
browserSettings.FileAccessFromFileUrls=CefState.Enabled;
browserSettings.UniversalAccessFromFileUrls=CefState.Enabled;
chromeBrowser.BrowserSettings=浏览器设置;
}
私有作废Form1\u FormClosing(对象发送方,FormClosingEventArgs e)
{
Cef.Shutdown();
}
公共空间开放()
{
if(Application.OpenForms.OfType().Count()==1)Application.OpenForms.OfType().First().Close();
Form3 frm=新Form3();
frm.ShowDialog();
frm.ebox=假;
GraphicsPath路径=新的GraphicsPath();
矩形路径矩形=新矩形(0,0,2000,2000);
AddRectangle(pathRect);
区域=新区域(路径);
frm.区域=区域;
}
公共作废按钮2\u单击(对象发送者,事件参数e)
{
if(Application.OpenForms.OfType().Count()==1)Application.OpenForms.OfType().First().Close();
Form3 frm=新Form3();
frm.Show();
frm.ebox=假;
GraphicsPath路径=新的GraphicsPath();
矩形路径矩形=新矩形(0,0,2000,2000);
AddRectangle(pathRect);
区域=新区域(路径);
frm.区域=区域;
}
私有void Form1\u FormClosed(对象发送方,FormClosedEventArgs e)
{
Application.Exit();
}
}
类自定义对象
{
//声明chromium的本地实例和主窗体,以便在主线程中从此处执行操作
私有静态ChromiumWebBrowser _instanceBrowser=null;
//表单类需要根据您的更改
私有静态Form1_instanceMainForm=null;
公共自定义对象(ChromiumWebBrowser原始浏览器,Form1主窗体)
{
_instanceBrowser=原始浏览器;
_instanceMainForm=mainForm;
}
public void showDevTools()
{
_instanceBrowser.ShowDevTools();
}
public void opencmd()
{
ProcessStartInfo start=newprocessstartinfo(“cmd.exe”,“c pause”);
进程启动(Start);
}
}
公共类JavaScriptInteractionBJ
{
公开作废openfrm()
{
Application.OpenForms.OfType().First().openup()文件
}
}
}
在上面的代码中,按钮2是使用设计器创建的。因此,当我点击按钮2时,表单3打开。
但在html页面中单击按钮时:
<button onclick="winformObj.openfrm();">Open</button>
打开
表单窗口将打开,但表单数据不会加载到窗口中
按钮2单击:
HTML按钮单击:
您现在可能已经解决了您的问题,但对于其他遇到此问题的人(如我所做的): 不能直接在从JavaScript调用的C#绑定函数中打开表单,因为它很可能会阻塞呈现过程 相反,您需要在UI线程上调用表单打开代码 以下是我的想法:
在C#类to bound的构造函数方法中,我传入保存浏览器的表单,以便调用它
// 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();
});
}
}
}
调试和发布错误消息(如果可用)以及一些代码将非常有用。@BadCode我已更新了必要的详细信息。请检查。您需要在UI线程上打开表单。还要考虑给表单提供描述性的名字,这将使你的生活更简单,即使你只是原型。你如何能够从主线程上的JavaScript访问Frim1?您的代码没有显示“CefCustomObject”的任何用法。我尝试使用Application.OpenForms,但JavaScript中出现一个错误,指出我的