The Imaginative Universal

Studies in Virtual Phenomenology -- @jamesashley

How to Quit a WP7 Silverlight Application

August 22
by James Ashley 22. August 2010 14:28
Customizing App.xaml.cs

In the current Beta dev tools for Windows Phone 7, there is no way to exit a Silverlight application (though there is a method to do this for XNA apps).

Currently the best way – and I use the term “best” loosely – to do this is to throw an exception.  An unhandled exception always manages to kill your application.

If we must use a hack, however, we might as well  do it with a bit of finesse.  Below is a simple implementation that can be placed in the App class of a WP7 Silverlight project that clarifies the behavior of the app when we intentionally throw an exception in order to exit rather than simply throwing an exception exceptionally.

This code can be called from anywhere in your app in order to Exit predictably.

Here is the call:
App.Quit();
And here is the implementation in App.xaml.cs:
private class QuitException : Exception { }

public static void Quit()
{
    throw new QuitException();
}

private void Application_UnhandledException(object sender, 
    ApplicationUnhandledExceptionEventArgs e)
{
    if (e.ExceptionObject is QuitException)
        return;

    if (System.Diagnostics.Debugger.IsAttached)
    {
        System.Diagnostics.Debugger.Break();
    }

    //etc.
}
Windows Phone 8 Update

If you are doing Windows Phone 8 development, things get easier.  After having so many apps going through the store using the above method to exit out, the WP Dev\Design team decided to just implement their own escape method.  Sometimes the doctor is right and sometimes the patient just has a better understanding of what the pain feels like.  To exit out of a Windows Phone 8 app, call Application.Terminate .

Tags:

Silverlight | Windows Phone | Customizing App.xaml.cs

Comments

10/19/2010 1:38:56 PM #

trackback

Exiting out of a WP7 Application

***** NOTE – As readers pointed out this method will NOT allow your application to be accepted into the

Derik Whittaker

10/19/2010 5:08:52 PM #

pingback

Pingback from vincenthomedev.wordpress.com

Exit out of a Windows Phone Application « Vincent Leung .NET Tech Clips

vincenthomedev.wordpress.com

10/24/2010 4:56:47 AM #

pingback

Pingback from recklessdevelopment.com

Exiting out of a WP7 Application | RecklessDevelopment

recklessdevelopment.com

10/27/2010 6:52:33 AM #

trackback

Дайджест технических материалов #5 (Windows Phone 7)

Tools, Books, Guides Windows Phone 7 Developer Tools RTM (online installer) , ISO UI Design and Interaction

Oleksandr Krakovetskiy blog

11/22/2010 3:51:53 AM #

pingback

Pingback from tech.plogie.com

Chileung's Tech Blog

tech.plogie.com

11/29/2010 9:15:29 PM #

pingback

Pingback from blogs.6bit.com

Exiting a Windows Phone Application

blogs.6bit.com

12/1/2010 6:28:18 AM #

trackback

Chiusura della propria app dopo la scadenza della trial

Chiusura della propria app dopo la scadenza della trial

Technology Experience (Reborn 3)

2/9/2011 5:17:36 PM #

azeaeeze

Are you serious ?
Windows Phone is sooooooo bad !
I can't believe my eyes.
Shame on microsoft !

azeaeeze France

5/28/2011 10:59:12 AM #

pingback

Pingback from blog.luo1002.com

Windows Phone 7 Tips (9) » NoName

blog.luo1002.com

6/8/2011 5:21:37 AM #

Christian Louboutin

Exit out of a Windows Phone Application « Vincent Leung .NET Tech Clips

Christian Louboutin United States

6/12/2011 10:14:49 PM #

pingback

Pingback from unknownerror.net

unknownerror.net

6/21/2011 12:31:12 PM #

pingback

Pingback from chaokuaidi.com

超快递 beta版 » Windows Phone 7 资源汇总(超全)

chaokuaidi.com

7/19/2011 10:46:26 PM #

Timmy williams

Thanks i found this helpful i will use it in my app development. Thanks again!!!!!!!!!!

Timmy williams United States

7/28/2011 4:53:05 AM #

pingback

Pingback from vangjee.wordpress.com

How to display an End User License Agreement (EULA) in Windows Phone 7 « Jee’s Blog

vangjee.wordpress.com

12/14/2011 12:12:17 PM #

pingback

Pingback from enzocontini.wordpress.com

Tips and Ticks | Enzo Contini Blog

enzocontini.wordpress.com

5/17/2012 11:10:03 PM #

pingback

Pingback from dancecoder.com

Windows Phone 7 资源汇总 | DanceCoder

dancecoder.com

11/12/2012 2:48:06 AM #

pingback

Pingback from joaquinbonet.com

Joaquin Bonet | Cerrando aplicaciones en WP8

joaquinbonet.com

Comments are closed