1
我们知道我们可以通过使用Application.Current.UnhandledException来在应用程序级别捕获任何意外的异常。有没有什么办法可以在页面级别而不是应用程序级别上捕获异常?Silverlight导航应用程序:如何在页面级别捕捉异常?
我们知道我们可以通过使用Application.Current.UnhandledException来在应用程序级别捕获任何意外的异常。有没有什么办法可以在页面级别而不是应用程序级别上捕获异常?Silverlight导航应用程序:如何在页面级别捕捉异常?
不是。一个页面并没有任何特定的执行范围。您可以随时在给定的屏幕上显示多个页面。但是,在Visual Studio中的Silverlight导航应用程序项目模板中,存在一个非常愚蠢的代码块,它会隐藏您的异常并将其变为无用的“导航失败”消息。
这是在Frame.NavigationFailed事件中引发异常,而导航到特定页面。但没有发生例外情况。如果你想访问异常,它在EventArgs中。
private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
e.Handled = true;
// the navigation template does this. useless
//ChildWindow errorWin = new ErrorWindow(e.Uri);
// this will show the exception
ChildWindow errorWin = new ErrorWindow(e.Exception);
errorWin.Show();
}