Showing posts with label tips. Show all posts
Showing posts with label tips. Show all posts

27 July 2012

Windows Phone 7.5 - Home button Navigation and Exit Confirmation

While developing a windows phone application, the one which involves navigation to various pages starting from something called as a homepage would quickly become annoying. You will find yourself in deeper pages and as a means on coming back to the home page, you will keep pressing the back button so many times it will exit you out of the application.

There are couple of problems associated with this. The user is forced to keep in mind how deep he has in the navigation page. The other main problem is he will not have patience to wait to go to the home page and pressing the back button on the first page will obviously throw you. This is because the navigation details are stored in a stack.

It is always nice to provide a home button to the end user on all the pages so that on click of them will take it to the home. Also, you can always have a confirmation dialogue fired while exiting the application. First problem first.



This code armed with a Home button will do the job cleanly for you.

private void Home_Click(object sender, System.Windows.RoutedEventArgs e)
{
    int depth = NavigationService.BackStack.Count();
    for (int i = 0; i < depth - 1; i++) { NavigationService.RemoveBackEntry(); }
    NavigationService.GoBack();
}

The funda here is it will empty all the stack and will call the back action of the navigation service just once making sure that the next back() will empty the stack and will make you quit.

As I told before adding this code to the MainPage.xaml.cs will show a confirmation popup while quitting the application so that user only deliberately quits but not accidentally.



protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    if (MessageBox.Show("Are you sure you want to exit?", "Exit?", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
    {
        e.Cancel = true;
    }
}

These techniques are very simple and subtle but will improve the usability of your application by leaps and bounds.

Cheers!
Braga

Windows Phone 7.5 WebBrowserTask - Simple Yet Powerful

WebBrowserTask is one of the most simple yet powerful tasks I found in Windows Mobile development. It is amazingly simple, just two or three lines of code and super tempting to include it in your project. Lets quickly see how to use it and places where you can utilize its capability at the presentation level.

Enough intro, here is the code

WebBrowserTask pptTask = new WebBrowserTask();
pptTask.Uri = new Uri("http://www.iasted.org/conferences/formatting/presentations-tips.ppt");
pptTask.Show();

Damn simple isnt it? You could use this in places where you need to launch or open an URL in the inbuilt Internet Explorer. And the best thing about this is it will automatically detect the attachment file types (most of them) and opens them cleanly. For example, it could read ppt, pdf, xls even videos and open them cleanly. You can make the integration look rich by providing a button in the Application bar. Following is a screenshot.



Besides opening simple URLs consider using this Task to open regular attachments and make your end application look extremely rich with just one or two lines of code.

Cheers!
Braga

19 July 2012

Exception Handling in Windows Phone 7 - When all else fails

I was recently testing something with Toast Notifications with Windows Phone 7.1. Visual Studio that I use has amazing debugging features like any other IDE available IDE out there. Here was scenario. I need to send two types of Notifications Raw and Toast Notifications to my mobile and need to assert scenarios. Debugging the Raw Notification was very simple. However, I was not able to debug/step into when I launch the application after clicking the toast Notifcation.

It was really painful not to be able see any exception message. It was very annoying. Then I found a way to at least know what is happening. To do that, you can just go the App.xaml.cs file which has all the main entry and exit points to the application. To read more about this, just google life cycle of a windows phone app.

All I had to do was 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();
    }
}

Problem Solved. Now I could see the error messages/exception stack trace in a neat Message box!

Cheers!
Braga