猿问

在 GeckoFX 中添加功能

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

繁星淼淼
浏览 208回答 1
1回答

隔江千里

此示例展示了从 javascript 到 C# 通信的两种不同方式。第一种方法是使用(哈顿方法)消息泵。第二种方法是使用javascript 更改location当前文档并使用C# 处理和取消导航。示例 HTML:<html><body></body><script type='text/javascript'>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; /* Begin (Hatton) method of communicating from javascript to C# */&nbsp; &nbsp; alert('javascript : about call message C# (Hatton) method');&nbsp; &nbsp; event = document.createEvent('MessageEvent');&nbsp; &nbsp; var origin = window.location.protocol + '//' + window.location.host;&nbsp; &nbsp; event.initMessageEvent ('callMe', true, true, 'some data', origin, 1234, window, null);&nbsp; &nbsp; document.dispatchEvent (event);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; alert('javascript : message sent to C# (Hatton) method');&nbsp; &nbsp; /* End (Hatton) method of communicating from javascript to C# */&nbsp; &nbsp; /* Begin javascript changing location.href method */&nbsp; &nbsp; alert('javascript : about call message C# location changed method');&nbsp; &nbsp; location.href='http://somehost//someevent.php?somedata';&nbsp; &nbsp; alert('javascript : message sent to C# location changed method');&nbsp; &nbsp; /* End javascript changing location.href method */</script></html>Example.cs 代码:using System;using System.IO;using System.Windows.Forms;using Gecko;&nbsp; &nbsp;&nbsp;namespace Example6{&nbsp; &nbsp; static class Example6&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private static GeckoWebBrowser _browser;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [STAThread]&nbsp; &nbsp; &nbsp; &nbsp; static void Main()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Don't use XULRunnerLocator.GetXULRunnerLocation() in production software.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string firefox14Path = XULRunnerLocator.GetXULRunnerLocation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Xpcom.Initialize(firefox14Path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var form = new Form();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _browser = new GeckoWebBrowser { Dock = DockStyle.Fill };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _browser.Navigating += InstallCustomEventListener;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _browser.Navigating += ListenForFakeNavigationMessages;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _browser.Navigate("file://" + Path.Combine(Environment.CurrentDirectory, "Example6.html"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form.Controls.Add(_browser);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Application.Run(form);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; #region Hatton method&nbsp; &nbsp; &nbsp; &nbsp; static void InstallCustomEventListener(object sender, GeckoNavigatingEventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _browser.Navigating -= InstallCustomEventListener;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _browser.AddMessageEventListener("callMe", ((string p) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show(String.Format("C# : Got Message '{0}' from javascript", p))));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; #endregion&nbsp; &nbsp; &nbsp; &nbsp; #region location changed method&nbsp; &nbsp; &nbsp; &nbsp; static void ListenForFakeNavigationMessages(object sender, GeckoNavigatingEventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const string id = "http://somehost//someevent.php?";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var uri = e.Uri.AbsoluteUri;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (uri.Contains(id))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show(String.Format("C# : Got Message '{0}' from javascscript", uri.Substring(id.Length)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // This is a fake navigating event - cancel it.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.Cancel = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; #endregion&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答