捕捉程序级应用时,必须先了解下生成的App.xaml.cs的代码。
一、
当创建一个Silverlight Windows Phone的工程项目时,一般IDE会包含以下几项:
项 | 描述 |
App.xaml / App.xaml.cs | 定义应用程序的入口点,初始化应用程序范围内的资源,,显示应用程序用户界面 |
MainPage.xaml / MainPage.xaml.cs | 定义应用程序中的程序页面(带有用户界面的页面) |
ApplicationIcon.png | 一种带有图标的图像文件,代表了手机应用程序列表中应用程序的图标 |
Background.png | 一种带有图标的图像文件,代表了在开始页面上应用程序的图表 |
SplashScreenImage.jpg | 这个图片会在应用程序第一次被启动时显示。启动画面会给用户一个即时的反馈,告诉用户应用程序正在启动直到成功跳转到应用程序的第一个页面。用户的启动画面可以和应用程序的一个页面设计的非常相似,这样能给使用这一个应用程序被快速加载的感觉。 |
Properties\AppManifest.xml | 一个生成应用程序包所必需的应用程序清单文件 |
Properties\AssemblyInfo.cs | 包含名称和版本的元数据,这些元数据将被嵌入到生成的程序集 |
Properties\WMAppManifest.xml | 一个包含与Windows Phone Silverlight应用程序相关的特定元数据的清单文件,且包含了用于Windows Phone的Silverlight所具有的特定功能 |
References folder | 一些库文件(集)的列表,为应用程序的工作提供功能和服务。 |
查看App.xaml.cs的代码,如下图:
public App()
{
// Global handler for uncaught exceptions.
UnhandledException += Application_UnhandledException;
// Standard Silverlight initialization
InitializeComponent();
// Phone-specific initialization
InitializePhoneApplication();
// Show graphics profiling information while debugging.
if (System.Diagnostics.Debugger.IsAttached)
{
// Display the current frame rate counters.
Application.Current.Host.Settings.EnableFrameRateCounter = true;
// Show the areas of the app that are being redrawn in each frame.
//Application.Current.Host.Settings.EnableRedrawRegions = true;
// Enable non-production analysis visualization mode,
// which shows areas of a page that are handed off to GPU with a colored overlay.
//Application.Current.Host.Settings.EnableCacheVisualization = true;
// Disable the application idle detection by setting the UserIdleDetectionMode property of the
// application's PhoneApplicationService object to Disabled.
// Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
// and consume battery power when the user is not using the phone.
PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
}
}
Application 类的 RootFrame 属性标识了应用程序的启动页面。 所有的Windows Phone应用程序都有一个最顶层的容器元素,它的数据类型是 PhoneApplicationFrame 。这个框架承载了一个或多个用来标识应用程序内容的 PhoneApplicationPage 元素,同时它还被用来处理不同页面之间的导航切换。
此时,需要创建一个事件句柄来处理UnhandledException事件。
二、向工程中添加一个新的 Windows Phone Portrait Page,并取名为ErrorPage.xaml
在页面中,添加控件用于展示错误信息。如下代码:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
<TextBlock x:Name="ApplicationTitle" Text="WINDOWS PHONE PUZZLE" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="error" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1">
<Border BorderBrush="White">
<TextBlock x:Name="ErrorText" Style="{StaticResource PhoneTextSmallStyle}" TextWrapping="Wrap" />
</Border>
</Grid>
</Grid>
添加完前台控件后,在后台绑定错误信息至前台,如下代码:
using System.Windows.Navigation;
public partial class ErrorPage : PhoneApplicationPage
{
public ErrorPage()
{
InitializeComponent();
}
public static Exception Exception;
// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ErrorText.Text = Exception.ToString();
}
}
三、关联程序 Application_UnhandledException 事件句柄。
在App.xaml.cs中,捕捉错误,并导向ErrorPage.xaml,
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break in the debugger
System.Diagnostics.Debugger.Break();
}
e.Handled = true;
ErrorPage.Exception = e.ExceptionObject;
(RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Source =
new Uri("/ErrorPage.xaml", UriKind.Relative);
}
总结:
这个Application_UnhandledException 就像一张安全网,应用程序中所有的不能被处理的异常都在这里终止。UnhandledException事件句柄处理完成后,它把所有的Handled属性都设置为true,这样做是为了阻止对异常的进一步处理。然后它把未处理异常的信息保存到ErrorPage 类的静态成员中,并设置帧的Source属性来显示错误页面。当设置Source属性的值与显示内容不同时,那么显示帧将会切换到一个新的内容。当导航切换到错误的页面后,它将会返回异常对象的文本值(Exception.ToString()) 并以此显示到页面中。一旦开始在真正设备上调试应用程序,这将会非常有用。
验证图: