【问题标题】:adding functions in GeckoFX在 GeckoFX 中添加功能
【发布时间】:2019-01-22 06:55:26
【问题描述】:

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

【问题讨论】:

    标签: c# html winforms geckofx


    【解决方案1】:

    这个example 展示了从 javascript 到 C# 的两种不同的通信方式。

    • 第一种方法是使用(哈顿方法)消息泵。
    • 第二种方式是使用javascript更改当前文档的location,并使用C#处理和取消导航。

    示例 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>
    

    Example.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
        }
    }
    

    也可以看看example 4

    【讨论】:

      猜你喜欢
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 2021-08-14
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多