Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Error Handling Mechanism in Windows Phone 7 Applications

In this article, we will learn how to manage with the exceptions in a Windows Phone 7 application.

Generally, when we tend to see the exception, there should be some sort of useful and meaningful detail or message displayed to the user that needs to be provided to the developer. This will assist the developer in finding the root cause of the issue or rather we should debug the issue. The bad thing or we could say the worst thing that could happen in such cases is the crash of the application and just get exit from it.

The crash of the application can be really irritating to the user and stop the end user to further use the same and can lead to that application being less popular. The example used in this article will make sure that the navigation is routed to the error page in case of the occurrence of an exception. But this is not the best solution to be displayed to the end user. The strategy should be such that a more generic message is displayed to the user giving him the option to notify the error which in turn will send an email to the respective address containing the exception details.

Step1. One needs to add a new Windows Phone Portrait Page to the Windows Phone 7 project in Visual Studio. Here in, we will be making use of the example naming the page as ‘Error.xaml’.

Step2. In ‘Error.xaml’, identify the ‘LayoutRoot’ Grid element, and then the task is to replace its content with the subsequent content:

<Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the application name and the page label-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
    <TextBlock x:Name="Title of the Application" Text="New Application" 
                    Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock x:Name="PageLabel" 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>

Step3. Now when we have created the page, it will behave as the container for any sort of exceptions that we would be displaying across.

The next thing that needs to be done is to open up ‘Error.xaml.cs’, and add the subsequent code:

using System.Windows.Navigation;
...
...

public static Exception Exception;

// This is implemented when the user is routed to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    ErrorText.Text = Exception.ToString();
}

Now, an exception object is being set up that is hooked up to ErrorText.Text on routing to the page.

Step4. The last step would be to catch an event handler that would make certain we routed tso this page at the time when an exception occurs that is unhandled.

This is made possible from ‘App.xaml.cs’. One needs to Open up‘App.xaml.cs’, and then the content of the ‘Application_UnhandledException’ function needs to be replaced with the subsequent code:

if (System.Diagnostics.Debugger.IsAttached)
{
    // An unhandled exception has occurred; break in the debugger
    System.Diagnostics.Debugger.Break();
}

// Executing on a emulator or device without the need of debugging
e.Handled = true;
Error.Exception = e.ExceptionObject;
(RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Source = 
 new Uri("/Error.xaml", UriKind.Relative); 

In case the above steps are completed without any issue being observed, then the moment an unhandled exception happens in your application, the application will be routed to this page and the exception stack trace will be displayed.

The verification of these steps would involve adding a button to the ‘MainPage.xaml’ and then get it linked to a page that never exists. Below is the example of the same:

NavigationService.Navigate(new Uri(“/NotHere.xaml”, UriKind.Relative)); 

The application needs to be launched now in the emulator and then the button needs to be clicked that is just created, (This needs to be launched without debugging). The outcome should be rerouted to ‘Error.xaml’ with the details of the exception.

Showing error exception

Figure 1: Showing error exception

Assuming we need to test something with Toast Notifications with Windows Phone 7.1. The Visual Studio that we normally make use of debugging features that are just outstanding. The features could be similar to any other IDE available out there. Let us see the scenario we are going to execute over here.

We need to send two sort of Notifications over here- one is Raw and the other would be toast Notifications to the mobile where we would be required to assert the scenarios. It is not difficult to debug the Raw Notification but it would not be easy to do the same when the application is launched after accessing the toast Notification.

In order to do this, one can just go the App.xaml.cs file that contains all the entry and exit points to the application. All you need to do is to add a message box to the method Application_UnhandledException in the App.xaml.cs.

private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
    MessageBox.Show(e.ExceptionObject.Message + "\n" + e.ExceptionObject.StackTrace);
    if (System.Diagnostics.Debugger.IsAttached)
    {
        // An unhandled exception has occurred; break into the debugger
        System.Diagnostics.Debugger.Break();
    }
}

Conclusion

The article concentrates on the error handling mechanisms for the Windows 7 phone applications and how to display a meaningful message in case of any exception occurs.



I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login