Windows Phone 7 异常的人性化处理

今天在学习 Windows Phone 7WP7() 编程时,接触到 WP7 的异常处理。
主要是异常的人性化显示。

在 App.xaml.cs 的 RootFrame_NavigationFailed (自动生成的) 函数中对 e.Handled 进行赋值。
先看未修改的代码:

1 private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)  
2  {  
3             if (System.Diagnostics.Debugger.IsAttached)  
4             {  
5                 // A navigation has failed; break into the debugger  
6                 System.Diagnostics.Debugger.Break();  
7             }  
8 }

修改后的代码:

private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)  
{  
    if (System.Diagnostics.Debugger.IsAttached)  
    {  
        // A navigation has failed; break into the debugger  
        System.Diagnostics.Debugger.Break();  
    }  
       
    e.Handled = true;  
    Page1.ExceptionInfo = e.Exception;  
    (RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Source = new Uri("/Page1.xaml", UriKind.Relative);  
}

将异常信息直接显示在 Page 1 页面的 Text 控件中。当然,如果为了让“用户”看懂异常信息,直接这样显示是不行的。需要将这种“计算机”语言转为自然语言。

其中,关键的一句是:

1 e.Handled = true;

如果没有此句,系统会将异常最终传递到:Application_UnhandledException() 函数中进行处理,并导致应用程序直接关闭。

 

posted @ 2016-02-22 11:08  91program  阅读(234)  评论(0编辑  收藏  举报