我有一个 C# 的 win 应用程序。我将文本以 HTML 格式保存在我的数据库中,但我想要在 WebBrowser 控件中向用户显示它。如何将字符串 HTML 内容显示到 WebBrowser 控件中?
试试这个:
webBrowser1.DocumentText =
"<html><body>Please enter your name:<br/>" +
"<input type='text' name='userName'/><br/>" +
"<a href='http://www.microsoft.com'>continue</a>" +
"</body></html>";
正如Thomas W.的评论所指出的一样-我差点没看到这条评论,但我也遇到了同样的问题,因此我认为值得重写为一个答案。
主要问题在于,在第一次将webBrowser1.DocumentText
分配给某些HTML之后,随后的分配没有任何效果。
解决方法可以在Thomas提供的链接http://weblogs.asp.net/gunnarpeipman/archive/2009/08/15/displaying-custom-html-in-webbrowser-control.aspx中找到详细信息,但是为防止以后该页面无法使用,我在下面进行总结。
简而言之,由于webBrowser控件的工作方式,每次更改内容时必须导航到新页面。因此,作者提出了一种更新控件的方法:
private void DisplayHtml(string html)
{
webBrowser1.Navigate("about:blank");
if (webBrowser1.Document != null)
{
webBrowser1.Document.Write(string.Empty);
}
webBrowser1.DocumentText = html;
}
然而我发现在我的当前应用程序中,我从 if(webBrowser1.Document != null)
这行代码中得到了一个CastException异常。我不确定这是为什么,但我发现如果我用try catch包装整个if
块,期望的效果仍然可以实现。参考:
private void DisplayHtml(string html)
{
webBrowser1.Navigate("about:blank");
try
{
if (webBrowser1.Document != null)
{
webBrowser1.Document.Write(string.Empty);
}
}
catch (CastException e)
{ } // do nothing with this
webBrowser1.DocumentText = html;
}
每次执行 DisplayHtml
函数时,我都会收到一个来自 if
语句的 CastException
异常,因此 if 语句的内容永远不会被执行。但是,如果我注释掉 if
语句以避免收到 CastException
,那么浏览器控件就无法更新。我怀疑文档属性背后的代码还有其他副作用,尽管它也会抛出异常。
总之,我希望这对人们有所帮助。
在处理展示给用户的内容之前,你应该将AllowNavigation属性设置为true。
- Ghasem不要导航到空白页面,可以执行以下操作:
而不是导航到空白页面,你可以做:
webBrowser1.DocumentText="0";
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write(theHTML);
webBrowser1.Refresh();
无需等待任何事件或其他事情。您可以查看MSDN中的OpenNew,而我已经在我的一个项目中测试了初始DocumentText分配并且它可以工作。
由于某些原因,m3z提供的代码(使用DisplayHtml(string)
方法)在我的情况下无法正常工作(除了第一次)。我总是从字符串中显示HTML。这是我与WebBrowser控件搏斗后的版本:
webBrowser1.Navigate("about:blank");
while (webBrowser1.Document == null || webBrowser1.Document.Body == null)
Application.DoEvents();
webBrowser1.Document.OpenNew(true).Write(html);
每次都为我工作。希望它能帮助别人。
Application.DoEvents
(); 解决了我的问题。我正在使用 webBrowser1.DocumentText = myHtml; while (webBrowser1.DocumentText != myHtml) { Application.DoEvents(); }
。 - gridtrak我尝试过的简单解决方案是:
webBrowser1.Refresh();
var str = "<html><head></head><body>" + sender.ToString() + "</body></html>";
webBrowser1.DocumentText = str;
这里有一段代码。它可以在WebBrowser控件的任何后续html代码更改时正常运行(适用于我)。您可以根据自己的特定需求进行调整。
static public void SetWebBrowserHtml(WebBrowser Browser, string HtmlText)
{
if (Browser != null)
{
if (string.IsNullOrWhiteSpace(HtmlText))
{
// Putting a div inside body forces control to use div instead of P (paragraph)
// when the user presses the enter button
HtmlText =
@"<html>
<head>
<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />
</head>
<div></div>
<body>
</body>
</html>";
}
if (Browser.Document == null)
{
Browser.Navigate("about:blank");
//Wait for document to finish loading
while (Browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
System.Threading.Thread.Sleep(5);
}
}
// Write html code
dynamic Doc = Browser.Document.DomDocument;
Doc.open();
Doc.write(HtmlText);
Doc.close();
// Add scripts here
/*
dynamic Doc = Document.DomDocument;
dynamic Script = Doc.getElementById("MyScriptFunctions");
if (Script == null)
{
Script = Doc.createElement("script");
Script.id = "MyScriptFunctions";
Script.text = JavascriptFunctionsSourcecode;
Doc.appendChild(Script);
}
*/
// Enable contentEditable
/*
if (Browser.Document.Body != null)
{
if (Browser.Version.Major >= 9)
Browser.Document.Body.SetAttribute("contentEditable", "true");
}
*/
// Attach event handlers
// Browser.Document.AttachEventHandler("onkeyup", BrowserKeyUp);
// Browser.Document.AttachEventHandler("onkeypress", BrowserKeyPress);
// etc...
}
}
虽然这是一个老问题,但以下是我在这种情况下的首选操作。
If browser.Document IsNot Nothing Then
browser.Document.OpenNew(True)
browser.Document.Write(My.Resources.htmlTemplate)
Else
browser.DocumentText = My.Resources.htmlTemplate
End If
请确保任何browser.Navigating
事件不要取消"about:blank" URL。以下是一个示例事件,以完全控制WebBrowser
导航。
Private Sub browser_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles browser.Navigating
Try
Me.Cursor = Cursors.WaitCursor
Select Case e.Url.Scheme
Case Constants.App_Url_Scheme
Dim query As Specialized.NameValueCollection = System.Web.HttpUtility.ParseQueryString(e.Url.Query)
Select Case e.Url.Host
Case Constants.Navigation.URLs.ToggleExpander.Host
Dim nodeID As String = query.Item(Constants.Navigation.URLs.ToggleExpander.Parameters.NodeID)
:
:
<other operations here>
:
:
End Select
Case Else
e.Cancel = (e.Url.ToString() <> "about:blank")
End Select
Catch ex As Exception
ExceptionBox.Show(ex, "Operation failed.")
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
webBrowser.NavigateToString(yourString);
m3z推荐的DisplayHtml(string html)对我很有用。
如果有人需要帮助,我还想提一下,最初我的HTML中有一些空格使HTML无效,因此文本显示为字符串。当我将HTML粘贴到Visual Studio时,这些空格被引入(在角括号周围)。因此,如果您尝试了本帖中提到的解决方案后,您的文本仍然显示为文本,则可能值得检查HTML语法是否正确。
更简单的是:
var html = "<html><body><h3>Hello</h3></body></html>";
BrowserCtrl.NavigateToString(html);
WebView
)有一个名为NavigateToString
的方法,您只需将HTML传递给它即可。 - BrainSlugs83