捕捉程式級應用時,必須先瞭解下產生的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())
並以此顯示到頁面中。一旦開始在真正裝置上調試應用程式,這將會非常有用。
驗證圖: