WP7/8退出程序

第一种办法,调用XNA中的退出方法,为了节约系统资源,XNA中提供了退出游戏的方法

  1. 添加对Microsoft.Xna.Framework.Game的引用;
  2. 调用Game.Exit()来退出程序。

但是请注意,不要使用该方法。因为该方法违反了微软的应用程序验证的规范,将会导致你的程序无法提交到Marketplace中去。

第二种方法,抛出自定义的Quit异常来退出程序

在App.xaml.cs文件中的App类添加如下代码:

private class QuitException : Exception { }

public static void Quit()

{

    throw new QuitException();

}

在App类的Application_UnhandledException方法中添加代码,使得它看起来如下:

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

    }

}

方法三:以上两种都通不过MS验证,现在新的方法是:

  protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)

        {

            int count = NavigationService.BackStack.Count();

            if (count > 0)

            {

                for (int i = 0; i < count; i++)

                {

                    NavigationService.RemoveBackEntry();

                }



            }

            base.OnNavigatedTo(e);

        }

 

详细参考:http://blog.jerrynixon.com/2011/11/mango-sample-exit-application.html

对于WP8可以直接使用:

Application.Current.Terminate();既可以。WP7bi比较麻烦而已!

你可能感兴趣的:(wp7)