【问题标题】:How to clear WebBrowser control in WPF如何清除 WPF 中的 WebBrowser 控件
【发布时间】:2014-02-27 17:49:52
【问题描述】:

我正在使用以下链接中的代码:Displaying html from string in WPF WebBrowser control

它运行良好,除非我删除包含 html 的项目,e.NewValue 变为 null 并且出现异常。有没有办法让 WebBrowser 控件返回空白屏幕?

【问题讨论】:

  • 我也觉得这很麻烦,因为Microsoft's documentation 本身声明“如果 text 参数为 nullWebBrowser 会导航到一个空白文档(“about:blank”)。”显然不是这样...还引用了“.NET Framework 自 3.0 起可用”,我在 4.0 上。

标签: c# wpf webbrowser-control


【解决方案1】:

我找到了这个。谁有更好的?

if (wb != null)
{
    if (e.NewValue != null)
        wb.NavigateToString(e.NewValue as string);
    else
        wb.Navigate("about:blank");
}

编辑:

正如 cmets 中提到的 poby,对于 .NET 4+ 使用:

if (wb != null)
{
    if (e.NewValue != null)
        wb.NavigateToString(e.NewValue as string);
    else
        wb.Navigate((Uri)null);
}

【讨论】:

  • 在 .Net 3.5 中:wb.Navigate(null) 而不是 wb.Navigate("about:blank")
  • 哦,太酷了,我得找个时间试试。感谢您的更新!
  • 我正在使用 .Net 4.0 和 wb.Navigate(null) 对于两个方法重载来说是模棱两可的。尝试 wb.Navigate(string.Empty) 也会失败,因为 string.Empty 是无效的 Uri。提供的解决方案就足够了,但我检查了!string.IsNullOrEmpty(e.NewValue.ToString())
  • 对于 .Net 4 及更高版本,使用 wb.Navigate((Uri)null) 可以避免歧义并产生魅力:)
【解决方案2】:

我只是将 WebBrowserLabel 的 DocumentText 设置为 string.Empty

【讨论】:

    猜你喜欢
    • 2013-11-26
    • 2017-08-31
    • 2015-06-13
    • 2011-01-03
    • 2016-10-14
    • 2012-08-14
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多