如何清除 WPF 中的 WebBrowser 控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22076375/
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
How to clear WebBrowser control in WPF
提问by mikelt21
I'm using the code from the following link: Displaying html from string in WPF WebBrowser control
我正在使用以下链接中的代码:Displaying html from string in WPF WebBrowser control
It works well except when I delete the item containing the html, e.NewValuebecomes nulland I get an exception. Is there a way to return the WebBrowser control to a blank screen?
它运行良好,除非当我删除包含 html 的项目时,e.NewValue变成null并且出现异常。有没有办法将 WebBrowser 控件返回到空白屏幕?
回答by mikelt21
I found this. Anyone have anything better?
我找到了这个。有人有更好的吗?
if (wb != null)
{
if (e.NewValue != null)
wb.NavigateToString(e.NewValue as string);
else
wb.Navigate("about:blank");
}
EDIT:
编辑:
As poby mentioned in the comments, for .NET 4+ use:
正如评论中提到的 poby,对于 .NET 4+ 使用:
if (wb != null)
{
if (e.NewValue != null)
wb.NavigateToString(e.NewValue as string);
else
wb.Navigate((Uri)null);
}
回答by ulatekh
I just set the WebBrowserLabel's DocumentTextto string.Empty.
我只是将 WebBrowserLabel 的DocumentText设置为string.Empty。

