WPF:捕获导航错误

来源:互联网 发布:关明生为什么离职知乎 编辑:程序博客网 时间:2024/05/17 20:08

WPF:在XBAP中捕获导航错误

(原文地址: http://www.cnblogs.com/mgen/archive/2012/01/13/2321728.html)


解决方案就是使用Application.DispatcherUnhandledException事件,这个事件可以侦听当前应用程序UI线程所有未处理异常,并可以将异常标记为处理从而使应用程序防止崩溃。记住同NavigationFailed事件一样,把Handled属性标记为true。

private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)

{

    MessageBox.Show(e.Exception.Message);

    e.Handled = true;

}


原文如下:

在普通WPF程序中本来是可以通过NavigationFailed事件的。(可以通过Application类,NavigationWindow类,NavigationService类,Frame类。它们都有NavigationService事件)。可以在XBAP中,这招貌似不能用。

 

比如随便弄了非法导航链接:

<Hyperlink NavigateUri="httppppp://www.a.com">link</Hyperlink>

 

然后加上Application.NavigationFailed事件:

protected override void OnNavigationFailed(NavigationFailedEventArgs e)

{

    base.OnNavigationFailed(e);

 

    MessageBox.Show(e.Exception.Message);

    e.Handled= true;

}

 

运行后点击链接,结果还是错误(IE8):

image

解决方案就是使用Application.DispatcherUnhandledException事件,这个事件可以侦听当前应用程序UI线程所有未处理异常,并可以将异常标记为处理从而使应用程序防止崩溃。记住同NavigationFailed事件一样,把Handled属性标记为true。

private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)

{

    MessageBox.Show(e.Exception.Message);

    e.Handled= true;

}


原创粉丝点击