2017-04-20 197 views
0

我已經加了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瀏覽器導航的工作,因此我真誠地希望你們中的一個可以說明這傻瓜。

+0

是否正在打印以打印到控制檯或物理打印機。 – Digvijay

+0

我正在打印到控制檯。我正在運行它在monoDevelop作爲控制檯應用程序 – Nulle

+0

檢查此答案:http://stackoverflow.com/questions/19734151/print-webbrowser-without-previewing-ie-single-click-print – Zsmaster

回答

3

OK,

我誤解了這個問題,它的一個Windows應用程序(的WinForms)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace ConsoleApp1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      PrintHelpPage(); 
      Console.ReadKey(); 
     } 

     public static void PrintHelpPage() 
     { 
      var th = new Thread(() => { 
       var br = new WebBrowser(); 
       br.DocumentCompleted += PrintDocument; 
       br.Navigate("http://google.com"); 
       Application.Run(); 
      }); 
      th.SetApartmentState(ApartmentState.STA); 
      th.Start(); 
     } 

     public static void PrintDocument(object sender, 
      WebBrowserDocumentCompletedEventArgs e) 
     { 
      var browser = sender as WebBrowser; 
      // Print the document now that it is fully loaded. 
      browser.Print(); 

      // Dispose the WebBrowser now that the task is complete. 
      browser.Dispose(); 
     } 
    } 
} 

的問題是,一個控制檯應用程序將不會觸發DocumentCompletedEvent除非你明確地將其標記STAThread像我一樣在線程中。

+0

我仍然收到空引用。 – Nulle

+1

請檢查更新的答案! – Digvijay

+0

我很抱歉我仍然收到空引用。你自己測試過了嗎? – Nulle

相關問題