Windows Phone click to exit the current application

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,Call the exit method in XNA. To save system resources, XNA provides a method to exit the game.:

  1. Add a reference to Microsoft. Xna. Framework. Game;
  2. Call Game. Exit () to Exit the program.

But please note that,Do not use this method. Because this method violates Microsoft's application verification specifications, your program cannot be submitted to the Marketplace.

Method 2,Throw a custom Quit exception to exit the program.:

Add the following code to the App class in the App. xaml. cs file:

Code Snippet
  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:

 

Code Snippet
  1. // Code to execute on Unhandled Exceptions
  2. Private void Application_UnhandledException (object sender, ApplicationUnhandledExceptionEventArgs e)
  3. {
  4. If (e. ExceptionObject is QuitException)
  5. Return;
  6. If (System. Diagnostics. Debugger. IsAttached)
  7. {
  8. // An unhandled exception has occurred; break into the debugger
  9. System. Diagnostics. Debugger. Break ();
  10. }
  11. }

 

 

Then make sure that the App. xmal has the UnhandledException setting:

Code Snippet
  1. UnhandledException = "Application_UnhandledException"

 

 

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

Related Article

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.