【发布时间】:2017-04-20 11:04:13
【问题描述】:
我已经花了超过 10 个小时试图找到一个通向 WebBrowser 属性的洞。
我只是想导航到 google.com 并将 html 代码打印到控制台。这不断给我一个空参考。
有很多与此相关的问题,他们都说需要 DocumentCompleted 事件处理程序或未正确设置。
所以我尽我所能尝试了这个,但仍然没有运气。然后我甚至从Microsoft复制粘贴官方示例我仍然得到空引用!
using System;
using System.Windows.Forms;
namespace Aamanss
{
class MainClass
{
public static void PrintHelpPage()
{
// Create a WebBrowser instance.
WebBrowser webBrowserForPrinting = new WebBrowser();
// Add an event handler that prints the document after it loads.
webBrowserForPrinting.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(PrintDocument);
// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri("https://www.google.com");
}
public static void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
// Print the document now that it is fully loaded.
((WebBrowser)sender).Print();
// Dispose the WebBrowser now that the task is complete.
((WebBrowser)sender).Dispose();
}
public static void Main(string[] args)
{
PrintHelpPage();
}
}
}
如您所见,上面的大部分代码都是从 Microsoft 复制粘贴的。这个错误:
Gtk-Message: Failed to load module "atk-bridge"
libgluezilla not found. To have webbrowser support, you need libgluezilla installed
System.NullReferenceException: Object reference not set to an instance of an object
at System.Windows.Forms.WebBrowser.Navigate (System.Uri url) [0x0000e] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:304
at System.Windows.Forms.WebBrowser.set_Url (System.Uri value) [0x00007] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:231
at (wrapper remoting-invoke-with-check) System.Windows.Forms.WebBrowser:set_Url (System.Uri)
at Aamanss.MainClass.PrintHelpPage () [0x00024] in /root/Projects/Aamanss/Aamanss/Program.cs:19
at Aamanss.MainClass.Main (System.String[] args) [0x00001] in /root/Projects/Aamanss/Aamanss/Program.cs:34
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
at System.Windows.Forms.WebBrowser.Navigate (System.Uri url) [0x0000e] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:304
at System.Windows.Forms.WebBrowser.set_Url (System.Uri value) [0x00007] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:231
at (wrapper remoting-invoke-with-check) System.Windows.Forms.WebBrowser:set_Url (System.Uri)
at Aamanss.MainClass.PrintHelpPage () [0x00024] in /root/Projects/Aamanss/Aamanss/Program.cs:19
at Aamanss.MainClass.Main (System.String[] args) [0x00001] in /root/Projects/Aamanss/Aamanss/Program.cs:34
我真的很想弄清楚您是如何使 WebBrowser Navigation 正常工作的,因此我真诚地希望你们中的某个人可以为傻瓜说明这一点。
【问题讨论】:
-
您是打印到控制台还是打印到物理打印机。
-
我正在打印到控制台。我在 monoDevelop 作为控制台应用程序运行它
-
您是否特别想为此使用 WebBrowser 控件?将 google.com html 打印到控制台有更简单的方法(例如
curl www.google.com),以及使用 c# 检索 HTML 的更简单方法(HttpWebRequest类) -
我知道但 Paul 但不知何故我发现 WebBrowser 在通过网络上的登录表单时更易于使用。我的最终目标是登录网络并将响应 html 复制到字符串中。
标签: c# winforms webbrowser-control nullreferenceexception