C#:"如何使用 WebBrowser 并将 html 打印到控制台." [英] C# : "How to use WebBrowser and print the html to the console. "
问题描述
我已经有 10 个小时的时间试图在 WebBrowser
属性中找到一个洞.
I have for plus 10 hours now tried to get a hole through to the WebBrowser
property.
我只是尝试导航到 google.com 并将 html 代码打印到控制台.这不断给我一个空引用.
I am simply trying to navigate to google.com and print the html code to the console. This constantly gives me a null reference.
有很多与此相关的问题,他们都说需要或未正确设置 DocumentCompleted Eventhandler.
There is alot of questions related to this and they all say that a DocumentCompleted Eventhandler is needed or not setup properly.
所以我尽我所能尝试了这个,但仍然没有运气.然后我什至从 Microsoft 我仍然得到空引用!
So i tried this to the best of my ability though still no luck. I then went so far as to copy paste the official example from Microsoft I am still getting the null reference!
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 复制粘贴的.这个错误:
As you can see the majority of the above code is copy pasted from Microsoft. And this errors:
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 正常工作的,因此我真诚地希望你们中的一个人可以为傻瓜说明这一点.
I am really desperate to figure out how you make a WebBrowser Navigation work in general and therefore i sincerely hope one of you can illustrate this for dummies.
推荐答案
好的,
我误解了它是一个 Windows 应用程序(Winforms)的问题
I misunderstood the problem that its a windows application (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.
the issue is that a console application would not fire the DocumentCompletedEvent unless you explicitly mark it STAThread as i did in the thread.
这篇关于C#:"如何使用 WebBrowser 并将 html 打印到控制台."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!