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.:
- Add a reference to Microsoft. Xna. Framework. Game;
- 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
- Private class QuitException: Exception {}
- Public static void Quit ()
- {
- Throw new QuitException ();
- }
Add code to the Application_UnhandledException method of the App class to make it look as follows:
Code Snippet
- // Code to execute on Unhandled Exceptions
- Private void Application_UnhandledException (object sender, ApplicationUnhandledExceptionEventArgs e)
- {
- If (e. ExceptionObject is QuitException)
- Return;
- If (System. Diagnostics. Debugger. IsAttached)
- {
- // An unhandled exception has occurred; break into the debugger
- System. Diagnostics. Debugger. Break ();
- }
- }
Then make sure that the App. xmal has the UnhandledException setting:
Code Snippet
- UnhandledException = "Application_UnhandledException"
Now, you only need to call the "App. Quit ()" method where you want to exit the program.