I'm writing a windows phone 7 app. I have "fatal exception" handling code where I know for sure that the app is totally busted and there's no point in continuing. (I'm hoping I never get here...). Since there's nothing more my app can do other than quit I want the user to be able to close the app.
But I noticed there is no System.Environment.Exit()
in the Silverlight 4 SDK for Windows Phone 7. Is there another way to quit the app programmatically?
App Cert Reqt:
5.1.2 Application Termination
The
application must handle exceptions
raised by the .NET Framework and not
terminate unexpectedly. During the
certification process, the application
is monitored for unexpected
termination. An application that
terminates unexpectedly fails
certification. When handling
exceptions, an application must
provide a user-friendly error message.
You may present a message that is
relevant to the context of the
application. The application must
continue to run and remain responsive
to user input after the exception is
handled. An application that displays
generic or unhelpful error messages
will fail certification.
I would recommend you provide any information you feel relevant to the user and then leave the navigation of the device to the user to manage in light of this.
Acknowledging known solutions to provide "Exit" buttons, currently I do not see a compelling reason to implement an "exit" from a WP7 application.
The platform is fully capable of managing closure of apps. The more apps don't provide an exit, the quicker users will become accustomed to not thinking about app house keeping, and let the platform manage it.
The user will just navigate their device using start, back, etc.
If the user wants out of the current app to go do something else quickly - easy - they just hit start.
.Exit(), whilst available for xna, really isn't required anymore either. There was a cert requirement during CTP that games had to provide an exit button. This is now gone.
Non game apps never had the need to implement this.
The more this topic's discussed (and it really has been given a good run around the block), the more the indicators to me suggest there is no need to code an exit.
For Windows Phone 8, simply call
App.Current.Terminate();
Navigate to App.xaml.cs
in your solution explorer and
add a static method to the App
class
public static void Exit()
{
App.Current.Terminate();
}
so that you can call it anywhere from your application , as below
App.Exit();
A "less ugly" (and apparently only) way to exit is outlined here. Yuck.
Here's how I do it:
void Exit()
{
while (NavigationService.BackStack.Any())
NavigationService.RemoveBackEntry();
base.OnBackKeyPress(new CancelEventArgs());
}
Unfortunately, this does not work :(
When you are trying to programatically quit a WP7 application, you need keep in mind the application certification requirements. Peter Torr has a blog post that can help with your approach. Paul Jenkins experienced issues with the MahTweets app in the Marketplace recently and he blogged about it here.
HTH,
indyfromoz
I suppose you can let your app throw an unhandled exception at which point, Windows Phone will automatically terminate your application.
As for certification requirement, you can ask for exception. This method has always worked for me.
App.Current.Terminate();
For Windows Phone 8.1 Silverlight Apps
App.Current.MainWindow.Close()