简体   繁体   中英

Application.Current.Exit in Windows Phone 8.1 doesn't terminate app

How should I "properly" programmatically terminate a Windows Phone 8.1 app? Application.Current.Exit (); doesn't work. My code continues right through it. Here's the snippet:

    public static async Task ShowAndGo (String MessCode, String MessText, Boolean Xit)
    {
        String Mess = "";                           // Start out with an empty Message to tell Joe User.
        String Title = "";                          // And an empty title too.



        MessageDialog messageDialog = new MessageDialog (Mess, Title);
        await messageDialog.ShowAsync ();           // Stop and wait for user to acknowledge.
        if (!Xit)                               // We're done here if we're not goin' down.
            return;

        //
        // If we're goin' down,
        // we have a lot of cleanup to do.
        //



        Application.Current.Exit ();                        // This should stop us. But it doesn't.
    }

How should I "properly" programmatically terminate a Windows Phone 8.1 app?

The short: There isn't a "proper" way, and it's recommend that you shouldn't

The remarks on the Application.Exit method states:

Normally, however, you should not provide this UI because the system automatically manages app lifetime and terminates suspended apps as needed to free resources.

The guidelines say you should:

Design your app to suspend when the user switches away from it and resume when the user switches back to it.

Don't terminate the app when the user navigates away from the app or closes the app using the close button. The operating system ensures that there is a consistent way for the user to access and manage apps. Your app is suspended when it is no longer visible to the user. By leaving the application lifecycle to the system, you ensure that your user can return to your app as efficiently as possible. Doing so also provides the best system performance and battery life from the device.

Rather register an event handler for the Suspending event.

It is called when the app is suspended. You can use this event handler to save relevant application and user data to persistent storage.

It is good to read the Application Lifecycle page.

How to programmatically terminate a Windows Phone 8.1 app?

Windows Phone 8.1

Application.Current.Exit();

Windows Phone 8.1 (Silverlight)

Application.Current.Terminate();

Your code

I used your code as is for testing, and it worked as expected when I remove:

if (!Xit)                               // We're done here if we're not goin' down.
        return;

Xit is never assigned in your code sample; if Xit is false then the termination line will not be reached. Just remember that bool values are false by default (if no value is given to them)

Try using Application.Current.Terminate(); instead of Application.Current.Exit ();

For example

private void Exit_Button(object sender, System.Windows.Input.GestureEventArgs e)
  {
    Application.Current.Terminate();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM