Exit Windows Phone Application

January 14, 2012

NoExitMany Windows Phone developers wonders how a Windows Phone Silverlight application can be closed?

The problem is that Microsoft didn’t provide any official way closing a Silverlight application, since working with the Metro style UX concept, the user should press the Back key, closing the application by himself.

You can argue with me about this concept, but this is not the place for discussing it. You may read further about it here.

This post is going to provide a simple and correct way of asking the user to close the application, leaving him no other option.

But first, you should know that technically, any Windows Phone application could be closed as following:

1. Throwing an exception from, let say: button click handler. The application’s unhandled-exception handler will catch it and silently close the application.

2. Create an instance of type Microsoft.Xna.Framework.Game and call Exit: new Microsoft.Xna.Framework.Game().Exit();

3. Clear the entire back-stack, and then click NavigationService.GoBack(). Again,. this will throw an exception saying the you can’t call GoBack while CanGoBack returns false.

– NOTE: There is no guarantee that Microsoft will like these options, and your application may be rejected by the Marketplace verification process.


The concept of asking the user to exit the application is simple. All you have to do is ask politely to press the back button for exit, lets say for maintenance or some other reason.

So here’s how:

1. In the place you want the user to exit, navigate to a special page, asking the user to click the Back button.

2. In this page navigated-to event handler, clear all entries from the back-stack, so pressing the back button will exit the application.

   1: protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)

   2: {

   3:     while (NavigationService.RemoveBackEntry() != null);


   5:     base.OnNavigatedTo(e);

   6: }

You can download the sample code from here.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



  1. JoshJanuary 15, 2012 ב 08:21

    Ahh, that is brilliant! I can also see how this would be userful in other ways as well, like in login forms. Before, you were supposed to have the app “load”, and then pop up the login screen “for the result”, and then close it when it finished. That way users could not use the back key to go back to the login screen.

    Now if I understand this correctly, I can just do a “no-pain” login screen and then call the script on the next page.

    Thanks so much!

  2. Tomer ShamamJanuary 15, 2012 ב 09:01

    Thanks Josh, indeed many developers have the same problem. I hope this solves it for good. 🙂