will
will

Reputation: 4063

Is there a way to programmatically quit my App? (Windows Phone 7)

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?

Upvotes: 11

Views: 12532

Answers (9)

indyfromoz
indyfromoz

Reputation: 3763

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

Upvotes: 0

Samet Dumankaya
Samet Dumankaya

Reputation: 1

App.Current.Terminate();

For Windows Phone 8.1 Silverlight Apps

Upvotes: 0

Karthik Krishna Baiju
Karthik Krishna Baiju

Reputation: 459

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();

Upvotes: 4

Saif Al Falah
Saif Al Falah

Reputation: 358

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.

Upvotes: 0

greensuisse
greensuisse

Reputation: 1827

For Windows Phone 8, simply call App.Current.Terminate();

Upvotes: 4

Carthbert
Carthbert

Reputation: 1

App.Current.MainWindow.Close()

Upvotes: -1

Jerry Nixon
Jerry Nixon

Reputation: 31813

Here's how I do it:

void Exit()
{
    while (NavigationService.BackStack.Any())
        NavigationService.RemoveBackEntry();
    base.OnBackKeyPress(new CancelEventArgs());
}

Unfortunately, this does not work :(

Upvotes: 2

Mick N
Mick N

Reputation: 14882

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.

Upvotes: 9

ctacke
ctacke

Reputation: 67178

A "less ugly" (and apparently only) way to exit is outlined here. Yuck.

Upvotes: 3

Related Questions