经历了webbrowser 内存溢出 排版错误等各种问题 终于找到了一个解脱模式,就是使用firefox内核的现阶段代替webbrowser最好的控件了。
在winform程序中,要在程序中展示一个web页面,最常用的就是.net自带的webbrowser,但是大家都知道它是IE,也知道IE是有多么强(er)大(bi)。而且微软已经宣布了IE的死亡。。。默哀1秒钟,继续正文
那么如何使用firefox呢?只需两步:
1、添加引用:Geckofx-Core、Geckofx-Winforms;图1
2、把xulrunner放在你项目的bin目录下;图2
图1
图2
上面两个资源的下载地址:https://bitbucket.org/geckofx/geckofx-33.0/downloads
ftp://ftp.mozilla.org/pub/xulrunner/releases/33.0/runtimes/
但是要注意一点:geckofx的版本要和xulrunner的版本对应,不然会有问题。xulrunner是firefox官网提供的,有最新版本,但是geckofx版本相对落后一些,但是也更新到了33.0版本,对应firefox的发布时间是2014年10月份,应该也够用了。
当把前面的资源配置好后,接下来就是调用api的活了,非常简单,上一段简单的代码。
新建一个winform程序,然后在默认的form1.cs文件中加入以下代码。
</>code
- public partial class Form1 : Form
- {
- private readonly string xulrunnerPath = Application.StartupPath + "/xulrunner";
- private const string testUrl = "https://www.alipay.com/";
- private GeckoWebBrowser Browser;
- public Form1()
- {
- InitializeComponent();
- Xpcom.Initialize(xulrunnerPath);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- Browser = new GeckoWebBrowser();
- Browser.Parent = this;
- Browser.Dock = DockStyle.Fill;
- Browser.Navigate(testUrl);
- }
- }
vb.net代码:
</>code
- Public Class Form1
- Inherits Form
- Private xulrunnerPath As String = (Application.StartupPath + "/xulrunner")
- Private Const testUrl As String = "https://www.alipay.com/"
- Private Browser As GeckoWebBrowser
- Public Sub New()
- MyBase.New
- InitializeComponent
- Xpcom.Initialize(Me.xulrunnerPath)
- End Sub
- Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
- Me.Browser = New GeckoWebBrowser
- Me.Browser.Parent = Me
- Me.Browser.Dock = DockStyle.Fill
- Me.Browser.Navigate(testUrl)
- End Sub
- End Class
OK,运行项目你将会看到支付宝的首页。
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛