在 Geckofx 网页浏览器中点击 link 将触发 Winforms 中的方法
Clicking on the link inside the Geckofx webbrowser will trigger the method in Winforms
有一个名为 "test.html" 的 HTML 文件,里面有两个 link。此HTML文件在geckoWebBrowser1
中显示如下:
<!DOCTYPE html>
<html>
<head lang="tr">
<meta charset="UTF-8">
<title>Test HTML</title>
</head>
<body>
<p><a href="#" id="open_file" class="open-file button" onclick="openFile()">Open A PDF file...</a></p>
<p><a href="#" id="go_to_articles" class="go-to-articles button" onclick="goToArticles()">Go to articles...</a></p>
</body>
<script>
function openFile()
{
// What should I write here?
}
function goToArticles()
{
// What should I write here?
}
</script>
</html>
这里是 winforms 内容:
using System;
using System.Windows.Forms;
using Gecko;
namespace Test
{
public partial class Frm1 : Form
{
public Frm1()
{
InitializeComponent();
Xpcom.Initialize("Firefox64");
}
private void Frm1_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
geckoWebBrowser1.Navigate("start\test.html");
}
public void OpenPDFFile()
{
var ofd = new OpenFileDialog { Filter = @"PDF |*.pdf", Title = @"Select a PDF file..." };
if (ofd.ShowDialog() == DialogResult.OK)
{
// Here, action will be taken regarding the selected file.
}
}
}
}
当我点击HTML文件中的Open A PDF file ...
link时,必须在Winforms中触发OpenPDFFile
方法并从对话框中选择PDF文件,但我没能这样做。但是我想通过单击 HTML 文件中的 Go to articles ...
link 来查看位于 Winforms 中的 "FrmArticles" 表单,但到目前为止我还没有实现。
来源于answer to the Error in 'AddMessageEventListener' on GeckoFX问题。
<!DOCTYPE html>
<html>
<head lang="tr">
<meta charset="UTF-8">
<title>Test HTML</title>
</head>
<body>
<p><a href="#" id="open_file" class="open-file button" onclick="fireEvent('openFiles', 'SomeData');">Open A PDF file...</a></p>
<p><a href="#" id="go_to_articles" class="go-to-articles button" onclick="goToArticles()">Go to articles...</a></p>
</body>
<script>
function fireEvent(name, data)
{
var event = new MessageEvent(name,{'view': window, 'bubbles': false, 'cancelable': true, 'data': data});
document.dispatchEvent(event);
}
</script>
</html>
Form.cs
内容,
using System;
using System.Windows.Forms;
using Gecko;
namespace Test
{
public partial class Frm1 : Form
{
public Frm1()
{
InitializeComponent();
Xpcom.Initialize("Firefox64");
}
private void Frm1_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
geckoWebBrowser1.Navigate("start\test.html");
AddMessageEventListener("openFiles", showMessage);
}
public void AddMessageEventListener(string eventName, Action<string> action)
{
geckoWebBrowser1.AddMessageEventListener(eventName, action);
}
private void showMessage(string s)
{
var ofd = new OpenFileDialog { Filter = @"PDF |*.pdf", Title = @"Select a PDF file..." };
if (ofd.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(ofd.FileName);
}
}
}
}
在示例中,您可以根据需要使用字符串 "SomeData" 作为参数。
有一个名为 "test.html" 的 HTML 文件,里面有两个 link。此HTML文件在geckoWebBrowser1
中显示如下:
<!DOCTYPE html>
<html>
<head lang="tr">
<meta charset="UTF-8">
<title>Test HTML</title>
</head>
<body>
<p><a href="#" id="open_file" class="open-file button" onclick="openFile()">Open A PDF file...</a></p>
<p><a href="#" id="go_to_articles" class="go-to-articles button" onclick="goToArticles()">Go to articles...</a></p>
</body>
<script>
function openFile()
{
// What should I write here?
}
function goToArticles()
{
// What should I write here?
}
</script>
</html>
这里是 winforms 内容:
using System;
using System.Windows.Forms;
using Gecko;
namespace Test
{
public partial class Frm1 : Form
{
public Frm1()
{
InitializeComponent();
Xpcom.Initialize("Firefox64");
}
private void Frm1_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
geckoWebBrowser1.Navigate("start\test.html");
}
public void OpenPDFFile()
{
var ofd = new OpenFileDialog { Filter = @"PDF |*.pdf", Title = @"Select a PDF file..." };
if (ofd.ShowDialog() == DialogResult.OK)
{
// Here, action will be taken regarding the selected file.
}
}
}
}
当我点击HTML文件中的Open A PDF file ...
link时,必须在Winforms中触发OpenPDFFile
方法并从对话框中选择PDF文件,但我没能这样做。但是我想通过单击 HTML 文件中的 Go to articles ...
link 来查看位于 Winforms 中的 "FrmArticles" 表单,但到目前为止我还没有实现。
来源于answer to the Error in 'AddMessageEventListener' on GeckoFX问题。
<!DOCTYPE html>
<html>
<head lang="tr">
<meta charset="UTF-8">
<title>Test HTML</title>
</head>
<body>
<p><a href="#" id="open_file" class="open-file button" onclick="fireEvent('openFiles', 'SomeData');">Open A PDF file...</a></p>
<p><a href="#" id="go_to_articles" class="go-to-articles button" onclick="goToArticles()">Go to articles...</a></p>
</body>
<script>
function fireEvent(name, data)
{
var event = new MessageEvent(name,{'view': window, 'bubbles': false, 'cancelable': true, 'data': data});
document.dispatchEvent(event);
}
</script>
</html>
Form.cs
内容,
using System;
using System.Windows.Forms;
using Gecko;
namespace Test
{
public partial class Frm1 : Form
{
public Frm1()
{
InitializeComponent();
Xpcom.Initialize("Firefox64");
}
private void Frm1_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
geckoWebBrowser1.Navigate("start\test.html");
AddMessageEventListener("openFiles", showMessage);
}
public void AddMessageEventListener(string eventName, Action<string> action)
{
geckoWebBrowser1.AddMessageEventListener(eventName, action);
}
private void showMessage(string s)
{
var ofd = new OpenFileDialog { Filter = @"PDF |*.pdf", Title = @"Select a PDF file..." };
if (ofd.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(ofd.FileName);
}
}
}
}
在示例中,您可以根据需要使用字符串 "SomeData" 作为参数。