我希望我的浏览器(GeckoFX)在点击我的网页中的按钮时执行.NET功能。我有以下代码:
public Form1()
{
InitializeComponent();
AddMessageEventListener("myFunction", ((string s) => this.showMessage(s)));
browser.LoadHtml
(
@"<!DOCTYPE html>
<html><head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">
<script type=""text/javascript"">
function fireEvent(name, data)
{
event = document.createEvent('MessageEvent');
event.initMessageEvent(name, false, false, data, null, null, null, null);
document.dispatchEvent(event);
}
</script>
</head>
<body>fdsfsda
<input type=""button"" onclick=""fireEvent('myFunction', 'some data');"" value=""SHOW DATA"" />
</body></html>"
);
}
public void AddMessageEventListener(string eventName, Action<string> action)
{
browser.AddMessageEventListener(eventName, action);
}
private void showMessage(string s)
{
MessageBox.Show(s);
}
但是在运行该代码时,行
browser.AddMessageEventListener(eventName, action);
给我一个'对象引用未设置为对象的实例'错误。我不明白,因为我已经检查了该行中的所有对象都已初始化。有人可以帮忙告诉我为什么会收到这个错误吗?
非常感谢!
答案 0 :(得分:0)
您可以尝试不同的方法。 订阅browser.DomClick事件 - 只要点击某些内容就会触发它。从那里,您可以过滤掉无趣的点击,只在点击所需元素时调用更多代码。
Browser.DomClick += browser_DomClick;
然后
private void browser_DomClick(object sender, DomMouseEventArgs e)
{
var browser = sender as GeckoWebBrowser;
if (sender != null && e != null && e.Target != null)
{
if (e.Button == GeckoMouseButton.Left)
{
HandleLeftClick(e);
}
if (e.Button == GeckoMouseButton.Middle)
{
HandleMiddleClick(e);
}
if (e.Button == GeckoMouseButton.Right)
{
HandleRightClick(e);
}
}
}
答案 1 :(得分:0)
尝试在 LoadHtml 之后添加 AddMessageEventListener ,如下所示:
browser.LoadHtml(@"<!DOCTYPE html>.....</html>");
AddMessageEventListener("myFunction", ((string s) => this.showMessage(s)));
并在LoadHtml中更改此行:
event = document.createEvent('MessageEvent');
event.initMessageEvent(name, false, false, data, null, null, null, null);
由此:
var event = new MessageEvent(name,{'view': window, 'bubbles': false, 'cancelable': false, 'data': data});
您的新代码如下:
public Form1()
{
InitializeComponent();
browser.LoadHtml
(
@"<!DOCTYPE html>
<html><head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">
<script type=""text/javascript"">
function fireEvent(name, data)
{
var event = new MessageEvent(name,{'view': window, 'bubbles': false, 'cancelable': false, 'data': data});
document.dispatchEvent(event);
}
</script>
</head>
<body>fdsfsda
<input type=""button"" onclick=""fireEvent('myFunction', 'some data');"" value=""SHOW DATA"" />
</body></html>"
);
AddMessageEventListener("myFunction", ((string s) => this.showMessage(s)));
}
private void showMessage(string s)
{
MessageBox.Show(s);
}
答案 2 :(得分:-1)
//这对我有用
public Form1()
{
InitializeComponent();
m_wb = new Gecko.GeckoWebBrowser();
m_wb.Dock = DockStyle.Fill;
//m_wb.AddMessageEventListener("func1", onFunc1); ->not work
this.Controls.Add(m_wb);
this.Load += Form1_Load;
private void Form1_Load(object sender, EventArgs e)
{
m_wb.LoadHtml(@"<!DOCTYPE html>
<html><head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">
<script type=""text/javascript"">
function fireEvent(name, data)
{
var event = new MessageEvent(name,{'view': window, 'bubbles': false, 'cancelable': false, 'data': data});
document.dispatchEvent(event);
}
</script>
</head>
<body>fdsfsda
<input type=""button"" onclick=""fireEvent('myFunction', 'some data');"" value=""SHOW DATA"" />
</body></html>"
);
m_wb.AddMessageEventListener("myFunction", ((string s) => MessageBox.Show(s)));