2017-04-20 2 views
0

私はプラス10時間でWebBrowserプロパティに穴を開けようとしました。C#: "WebBrowserを使用してhtmlをコンソールに出力する方法"

私は単にgoogle.comに移動し、htmlコードをコンソールに出力しようとしています。これは常に私にヌル参照を与えます。

これに関連して多くの質問があり、DocumentCompleted Event Handlerが必要であるか正しく設定されていないと言われています。

だから私はまだ運がないのに、私の能力の中でこれを試しました。私はそれまで公式の例を貼り付けてコピーするまで行った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 

私はあなたが一般的でWebBrowserナビゲーション作業を行うため、私は心からあなたの1はダミーのためにこれを説明することを願っ方法を見つけ出すことは本当に必死です。 OK

+0

は、コンソールまたは物理プリンタに印刷するために印刷していますか。 – Digvijay

+0

私はコンソールに印刷しています。私はコンソールアプリケーションとしてmonoDevelopでそれを実行しています – Nulle

+0

この回答を確認してください:http://stackoverflow.com/questions/19734151/print-webbrowser-without-previewing-ie-single-click-print – Zsmaster

答えて

3

は私が問題を誤解しているの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(); 
     } 
    } 
} 

問題を明示的に私が行ったように、それはSTAThreadマークしない限り、コンソールアプリケーションがDocumentCompletedEventを発射しないことですスレッド内で

+0

私はまだヌル参照を取得しています。 – Nulle

+1

check updated答えてください! – Digvijay

+0

申し訳ありませんが、まだnull参照が取得されています。あなた自身でこれをテストしましたか? – Nulle

関連する問題