WP Program-Level Error capture and navigate to the error page

Source: Internet
Author: User

When capturing program-level applications, you must first understand the generated app. XAML. CS code.

I,

When creating a Silverlight Windows Phone project, IDE generally includes the following items:

Item

Description

App. XAML/APP. XAML. CS

Define application entry points, initialize resources within the application scope, and display the Application User Interface

Mainpage. XAML/mainpage. XAML. CS

Define the application page (page with user interface) in the application)

Applicationicon.png

An image file with icons, representing the icons of applications in the mobile app list

Background.png

An image file with icons represents the charts of the application on the start page.

Splashscreenimage.jpg

This image is displayed when the application is started for the first time. The startup screen will give you an immediate feedback, telling the user that the application is starting until it is successfully redirected to the first page of the application. The user's startup screen can be very similar to the design of a page of the application, so that the application can be quickly loaded.

Properties \ appmanifest. xml

An Application List file required to generate an application package

Properties \ assemblyinfo. CS

Metadata containing the name and version will be embedded into the generated assembly

Properties \ wmappmanifest. xml

A configuration file that contains specific metadata related to the Windows Phone Silverlight application and contains specific functions of Silverlight for Windows Phone.

References folder

Lists of database files (sets) to provide functions and services for application work.

View the code of APP. XAML. CS, such:

 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;            }        }

ApplicationClassRootframeThe launch page of the application. All Windows Phone applications have a top-level container element whose data type isPhoneapplicationframe. This framework carries one or morePhoneapplicationpageIt is also used to process navigation switching between different pages.

In this case, you need to create an event handle for processing.UnhandledexceptionEvent.

II,Add a newWindows Phone portrait pageAnd namedErrorpage. XAML

On the page, add a control to display the error message. The following code:

<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>

After the front-end control is added, bind the error message to the front-end at the backend. The Code is as follows:

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();  }}

3. associated programApplication_unhandledexceptionEvent handle.

In app. XAML. CS, capture errors and direct them to 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);}

Summary:

ThisApplication_unhandledexceptionLike a security net, all exceptions that cannot be handled in the application are terminated here.UnhandledexceptionAfter the event handle is processedHandledThe attribute is set to true to prevent further exception handling. Then it saves the information of the unprocessed exceptionErrorpageAnd set the frameSourceAttribute to display the error page. When the value of the source attribute is different from the display content, the display frame is switched to a new content. When the navigation switch to the wrong page, it will return the text value of the exception object (Exception. tostring ())
And then displayed to the page. Once you start to debug the application on a real device, this will be very useful.

Verification diagram:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.