GeckoFX浏览器:从JavaScript调用c#函数

GeckoFX browser: Call C# function from JavaScript

本文关键字:调用 函数 JavaScript 浏览器 GeckoFX      更新时间:2023-09-26

我试图在数据(p)上调用MessageBox.Show(p)函数,我从GeckoWebBrowser的MessageEvent中获得。不管用!帮助吗?
(这应该会显示一个消息框,上面写着"test")

Javascript:

function callServerFunction(fname, parameter) {
    event = new MessageEvent(fname, { 'view': window, 'bubbles': false, 'cancelable': false, 'data': parameter });
    document.dispatchEvent(event);
}
function fillClientsTable() {
    callServerFunction("fillClientsTable", "test");
}
window.onload = function () {
    fillClientsTable();
};
c#:

private void Form1_Load(object sender, EventArgs e) {
    browser.AddMessageEventListener("fillClientsTable", (string p) => MessageBox.Show(p));
}


附注:测试时,函数onload(), fillClientsTable()和callServerFunction()都被调用!

我真傻,原来Form1_Load没有被调用!不知道为什么,但我删除了它,并从事件列表中重新添加。
现在一切正常了:)