我已經加了10個小時,現在試圖通過WebBrowser
屬性獲得一個洞。C#:「如何使用WebBrowser並將html打印到控制檯。」
我只是試圖導航到google.com並將html代碼打印到控制檯。這不斷給我一個空引用。
有很多與此相關的問題,他們都表示需要或不正確設置DocumentCompleted Eventhandler。
所以我盡我所能,儘管仍然沒有運氣。然後我竟然複製了粘貼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();
}
}
}
正如你可以看到上述代碼的大部分是從微軟複製粘貼。而這個錯誤:
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
我真的絕望弄清楚如何讓一般的web瀏覽器導航的工作,因此我真誠地希望你們中的一個可以說明這傻瓜。
是否正在打印以打印到控制檯或物理打印機。 – Digvijay
我正在打印到控制檯。我正在運行它在monoDevelop作爲控制檯應用程序 – Nulle
檢查此答案:http://stackoverflow.com/questions/19734151/print-webbrowser-without-previewing-ie-single-click-print – Zsmaster