当地址为 "about:blank" 时,如何在 WebBrowser 控件中显示 HTML 字符串

How to show an HTML string in WebBrowser control when the address is "about:blank"

我想在 Webbrowser 控件导航到 "about:blank" 时写入一个特定的字符串(例如帮助说明),我可以使用 [=14= 在 Form_Load 中写入我的字符串] 并自动导航到 "about:blank"

webBrowser1.DocumentText = "introduction....";

但现在如果用户刷新 webbrowser 控件,它会显示一个空白页面。我希望它在地址为 "about:blank" 时再次显示我的字符串。将我的字符串放入网络浏览器控件的最佳位置在哪里?

文档Refresh simply reloads the current page, so the Navigating, Navigated, and DocumentCompleted events do not occur when you call the Refresh方法。

使用 NavigatingNavigated 事件,您应该检查浏览器是否正在导航或导航到 about:blank,然后禁用用户刷新页面的方式,包括浏览器快捷方式、浏览器上下文菜单或您创建或刷新的自定义工具栏按钮和上下文菜单等任何其他点。

对于其他网址,请重新启用它们。

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    var state = (e.Url.ToString().ToLower() == "about:blank");
    this.webBrowser1.WebBrowserShortcutsEnabled = !state;
    this.webBrowser1.IsWebBrowserContextMenuEnabled = !state;
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    var content = "Custom Content";
    if (e.Url.ToString().ToLower() == "about:blank" &&
        this.webBrowser1.DocumentText != content)
    {
        this.webBrowser1.DocumentText = content;
    }
}