本文介绍了在 GeckoFX 中添加功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 C# Winform 中制作类似 Steam 的程序启动器,而对于 UI,我正在使用 HTML 和 Gecko
我想知道是否可以添加我自己的 javascript 或 onclick() 函数并将其连接到我的 Windows Form C# 应用程序中例如,我需要在按钮 (HTML) 中添加一个函数来激活 c# 中启动程序的函数(它是一个获取应用程序名称并启动该应用程序的函数).

I am making an steam like program launcher in C# Winform and for UI I'm using HTML and Gecko
I am wondering if it is possible to add my own javascript or onclick() function and wire that in my Windows Form C# appfor example, I need to add a function in button (HTML) to activate function in c# that launches a program (it's a function that gets app name and launches that app).

推荐答案

这个 示例 展示了从 JavaScript 到 C# 通信的两种不同方式.

This example shows two different ways one can communicate from javascript to C#.

  • 第一种方法是使用(哈顿方法)消息泵.
  • 第二种方式是使用javascript改变当前文档的location,使用C#来处理和取消导航.
  • The first way is by using the (Hatton method) message pump.
  • the second way is by using javascript to change location of the current document and using C# to handle and cancel the navigation.

示例 HTML:

<html>
<body>
</body>
<script type='text/javascript'>

    /* Begin (Hatton) method of communicating from javascript to C# */

    alert('javascript : about call message C# (Hatton) method');
    event = document.createEvent('MessageEvent');
    var origin = window.location.protocol + '//' + window.location.host;
    event.initMessageEvent ('callMe', true, true, 'some data', origin, 1234, window, null);
    document.dispatchEvent (event);
    alert('javascript : message sent to C# (Hatton) method');

    /* End (Hatton) method of communicating from javascript to C# */

    /* Begin javascript changing location.href method */

    alert('javascript : about call message C# location changed method');
    location.href='http://somehost//someevent.php?somedata';
    alert('javascript : message sent to C# location changed method');

    /* End javascript changing location.href method */
</script>
</html>

示例.cs 代码:

using System;
using System.IO;
using System.Windows.Forms;
using Gecko;
namespace Example6
{
    static class Example6
    {
        private static GeckoWebBrowser _browser;

        [STAThread]
        static void Main()
        {
            // Don't use XULRunnerLocator.GetXULRunnerLocation() in production software.
            string firefox14Path = XULRunnerLocator.GetXULRunnerLocation();
            Xpcom.Initialize(firefox14Path);

            var form = new Form();
            _browser = new GeckoWebBrowser { Dock = DockStyle.Fill };
            _browser.Navigating += InstallCustomEventListener;
            _browser.Navigating += ListenForFakeNavigationMessages;

            _browser.Navigate("file://" + Path.Combine(Environment.CurrentDirectory, "Example6.html"));

            form.Controls.Add(_browser);
            Application.Run(form);
        }

        #region Hatton method
        static void InstallCustomEventListener(object sender, GeckoNavigatingEventArgs e)
        {
            _browser.Navigating -= InstallCustomEventListener;
            _browser.AddMessageEventListener("callMe", ((string p) =>
                MessageBox.Show(String.Format("C# : Got Message '{0}' from javascript", p))));
        }
        #endregion

        #region location changed method
        static void ListenForFakeNavigationMessages(object sender, GeckoNavigatingEventArgs e)
        {
            const string id = "http://somehost//someevent.php?";
            var uri = e.Uri.AbsoluteUri;
            if (uri.Contains(id))
            {
                MessageBox.Show(String.Format("C# : Got Message '{0}' from javascscript", uri.Substring(id.Length)));

                // This is a fake navigating event - cancel it.
                e.Cancel = true;
            }
        }
        #endregion
    }
}

另请参阅示例 4

这篇关于在 GeckoFX 中添加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:21