In this tutorial, you will learn
how to update the application to display an error page whenever an Unhandled
Exceptionoccurs in the application. The task shows how to handle the
errors as well as how to add pages to your application and navigate between
them. This post is a part of our Windows Phone tutorial series.
1. Add a new page to the project.
In Solution Explorer, right-click the HelloPhone project
option, and Add New Item. In
the Add New Item dialog, select Windows
Phone Portrait Page from the list of templates given, set the
name to ErrorPage.xaml and then click Add.
... <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitleGrid is the name of the application and page title--> <Grid x:Name="TitleGrid" Grid.Row="0"> <TextBlock Text="Windows Phone 7 Series" x:Name="textBlockPageTitle" Style=" {StaticResource PhoneTextPageTitle1Style}"/> <TextBlock Text="error" x:Name="textBlockListTitle" Style=" {StaticResource PhoneTextPageTitle2Style}"/> </Grid> <Grid x:Name="ContentGrid" Grid.Row="1"> <Border BorderBrush="White"> <TextBlock x:Name="ErrorText" Style=" {StaticResource PhoneTextSmallStyle}" TextWrapping="Wrap" /> </Border> </Grid> </Grid> ...
3. Press F7 to open the code-behind file of the new page or, right-click ErrorPage.xaml in Solution Explorer and select View Code. Then, insert the following code snippet into the ErrorPage class at the highlighted location. This sets up an Exception object that is tied up to the ErrorText.
public partial class ErrorPage : PhoneApplicationPage { public ErrorPage() { InitializeComponent(); } public static Exception Exception; // Executes when the user navigates to this page. protected override void OnNavigatedTo(Microsoft.Phone.Navigation.PhoneNavigationEventArgs e) { ErrorText.Text = Exception.ToString(); } }
4. After that , hook up an event handler to navigate to the error page and display an error message whenever an unhandled exception occurs. In Solution Explorer, right-clickApp.xaml and select View Code to open the code-behind class of the Application class.
5. Find the Application_UnhandledException event handler and insert the following code snippet at the highlighted location immediately before the closing brace. TheApplication_UnhandledException is a safety net where all unhandled exceptions of your application end up. Now you hook up the exception object to the ErrorPage.Exceptionobject and when you browse to the error page, it takes the exception object’s text value (Exception.ToString();) and displays it on the page. This will be very useful once you start debugging your application on a real device.
// Code to execute on Unhandled Exceptions 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); }
No comments
Post a Comment