Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF中清除WebBrowser控件_C#_Wpf_Webbrowser Control - Fatal编程技术网

C# 如何在WPF中清除WebBrowser控件

C# 如何在WPF中清除WebBrowser控件,c#,wpf,webbrowser-control,C#,Wpf,Webbrowser Control,我正在使用以下链接中的代码: 它工作得很好,除了当我删除包含html的项目时,e.NewValue变为null,我得到一个异常。有没有办法将WebBrowser控件返回到空白屏幕?我找到了这个。有更好的吗 if (wb != null) { if (e.NewValue != null) wb.NavigateToString(e.NewValue as string); else wb.Navigate("about:blank"); } 编辑

我正在使用以下链接中的代码:


它工作得很好,除了当我删除包含html的项目时,
e.NewValue
变为
null
,我得到一个异常。有没有办法将WebBrowser控件返回到空白屏幕?

我找到了这个。有更好的吗

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

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

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

我只是在.Net 3.5中将WebBrowserLabel设置为.

中的wb.Navigate(null)而不是wb.Navigate(“about:blank”)。哦,太酷了,我以后一定要尝试一下。谢谢你的更新!我使用的是.NET4.0和
wb.Navigate(null)
对于其中两个方法重载是不明确的。尝试
wb.Navigate(string.Empty)
也会失败,因为
string.Empty
是无效的Uri。提供的解决方案就足够了,不过我检查了
!string.IsNullOrEmpty(e.NewValue.ToString())
对于.NET4和更高版本,使用
wb.Navigate((Uri)null)
避免了歧义并使用了一个符咒:)我也发现了这个问题,考虑到它本身声明“如果文本参数为null,则导航到一个空白文档(“about:blank”)。”显然不是这样的。。。还引用了“从3.0开始提供.NET框架”,我使用的是4.0。