Щелчок по ссылке внутри веб-браузера Geckofx вызовет метод в Winforms - PullRequest
0 голосов
/ 01 февраля 2020

Имеется файл HTML с именем "test. html" и двумя ссылками. Этот 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.
            }
        }
    }
}

Когда я нажимаю ссылку Open A PDF file ... в HTML file, метод OpenPDFFile должен быть запущен в Winforms и выбран PDF-файл из диалогового окна, но я не смог этого сделать. Тем не менее, я хотел бы видеть форму «FrmArticles», расположенную в Winforms, нажав на ссылку Go to articles ... в файле HTML, но я пока не смог ее достичь.

1 Ответ

1 голос
/ 01 февраля 2020

Он получен из ответа на ошибку в 'AddMessageEventListener' в вопросе 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 content,

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» в качестве параметра, если хотите.

...