wpf 如何将 System.Windows.Control WebBrowser.Document 转换为 mshtml.MSHTMLDocumentClass?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21466273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:40:35  来源:igfitidea点击:

How do I cast a System.Windows.Control WebBrowser.Document to an mshtml.MSHTMLDocumentClass?

c#html.netwpfmshtml

提问by Adam

I have a WebBrowserthat loads inside a WPF window. I need to get the title of the web page loaded in the WebBrowser.

WebBrowser在 WPF 窗口中加载了一个。我需要获取加载在WebBrowser.

I get the document using

我使用

object doc = this._browser.Document;and I can see that it is an mshtml.MSHTMLDocumentand I want to cast it as this type so that I can pull the title out, however I can't find this type in any .NET library.

object doc = this._browser.Document;我可以看到它是 anmshtml.MSHTMLDocument并且我想将它转换为这种类型,以便我可以拉出标题,但是我在任何 .NET 库中都找不到这种类型。

Will I have to create the type myself or am I just looking in the wrong place/approaching this wrong way?

我是否必须自己创建类型,还是我只是在错误的地方/以错误的方式接近?

How can I pull the page title out of a System.Windows.Controls.WebBrowserDocument?

如何从System.Windows.Controls.WebBrowser文档中拉出页面标题?

回答by dkozl

Either add reference to Microsoft.mshtmland then:

添加对Microsoft.mshtml然后的引用:

var title = (webBrowser.Document as mshtml.HTMLDocument).title;

or

或者

dynamic doc = webBrowser.Document;
var title = doc.title;