WP7 -- How to exit your program?

Source: Internet
Author: User

You may notice that in the Silverlight for Windows Phone program, there are no functions similar to "app. Exit ()" to let you exit the program. What's the problem?

In Windows Phone 7, the system requires a hardware "back" key, which is used to navigate (return) to the previous page (screen) or application in the program. When a menu, dialog box, search box, or virtual keyboard is opened, click it to close the menu, dialog box, search box, and virtual keyboard.

When the application stays on the first interface of the program, if you press the return key, the program will automatically close and return to the previous interface to open the program. Because this action is the default method used by the system to close the program, you do not need to force quit the program in the code.Therefore, Microsoft does not leave a function to exit the program for you to call.

If... Well, I mean, what if you have to close the program in the code? No way.

The first method is to call the exit method in xNa. In order to save system resources, xNa provides a method to exit the game:

Add a reference to Microsoft. xNa. Framework. Game. Call game. Exit () to exit the program.However, do not use this method. Because this method violates Microsoft's application verification specifications, your program cannot be submitted to the marketplace.

The second method throws a custom quit exception to exit the program: (however, this method has many online objections and is not recommended .)

Add the following code to the app class in the app. XAML. CS file:

1.private class QuitException : Exception { }2.public static void Quit()3.{4.    throw new QuitException();5.}

 

Add code to the application_unhandledexception method of the app class to make it look as follows:

2.private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)3.{4.    if (e.ExceptionObject is QuitException)5.        return;6.7.    if (System.Diagnostics.Debugger.IsAttached)8.    {9.        // An unhandled exception has occurred; break into the debugger10.        System.Diagnostics.Debugger.Break();11.    }12.}

 

 

Then make sure that the app. Xmal. CS constructor has the unhandledexception processing:

Unhandledexception + = "application_unhandledexception"

Now, you only need to call the "app. Quit ()" method where you want to exit the program.

 

Method 3: add this method on the page you want to exit

Add the code to the onnavigatedto event of yourpage. XAML. CS:

protected override void OnNavigatedTo(NavigationEventArgs e)        {            int count = NavigationService.BackStack.Count();            if (count > 0)            {                for (int i = 0; i < count; i++)                {                    NavigationService.RemoveBackEntry();                }            }            base.OnNavigatedTo(e);        }

In this way, the program will exit normally as soon as you press the back key on this page. Note that the navigationservice. backstack and removebackentry must be placed in the onnavigatedto event for display.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.