locked
How can i exit my Silverlight WP7 application? RRS feed

  • Question

  • Hi, i am creating a Silverlight app on WP7, but i cant find any code to exit my application (like application.exit in previous versions of windows phone) . Is there any way to exit the application other than just using the hardware back button?


    Thanks, Manu

    Thursday, September 30, 2010 2:28 PM

All replies

  • I've not seen any application that will allow a Silverlight based Windows Phone application to terminate itself, though I have seen some programmatically have the application to navigate back beyond it's first page to make it terminate. 

    Thursday, September 30, 2010 3:12 PM
  • Hi Manu,

    Joel is right...

    I'm afraid the WP 7 framework doesn't provide any Application.Exit() method as you mentionned.
    Exit method is only available in the XNA framework, like Game.Exit() to exit a Game app.

    Peter Torr covers the following scenarios on his blog:

    1.An XNA application can call Game.Exit() to exit the game.
    2.The user can exit a Silverlight application by hitting the Back button on the first page of the application.
    3.The user can pause any application by hitting the Start button on any page of your application
    (or taking another action that switches to another application, such as tapping on a toast message).
    4.An unhandled exception in any application will cause the application to be killed

    http://blogs.msdn.com/b/ptorr/archive/2010/08/01/exiting-a-windows-phone-application.aspx

    Hope this helps.

    Sunday, October 3, 2010 2:28 PM
  • Hi,

    I have seen some programmatically have the application to navigate back beyond it's first page to make it terminate

    Will you please tell me how can I do that?

    Thursday, December 9, 2010 2:04 AM
  • Just code a .GoBack with no try/catch to raise an unhandled error, but please comment the code!

    Thursday, December 9, 2010 4:12 AM
  • Also make sure your Isolated storage gets saved IsolatedStorage.ApplicationSettings.Save() as I don't like leaving that to chance when throwing an unhandled error <- defensive programming.

    Thursday, December 9, 2010 4:14 AM
  • Hi Xpert360,

    Thanks for the exit tips. I was searching for the same, and found this.

    Useful info.

    Thursday, December 9, 2010 4:31 AM
  • The answer is fairly simple, just do the following:

    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
    e.Cancel = true;
    MessageBoxResult res = MessageBox.Show("Are you sure you want to exit?", "Exit?", MessageBoxButton.OKCancel);
    if (res == MessageBoxResult.OK)
    {
    e.Cancel = false;
    base.OnBackKeyPress(e);


    }
    }

    Visit http://austinejei.blogspot.com/2012/06/exiting-wp7-application.html for more information.

    Friday, June 22, 2012 7:48 AM
  • Hi, you can try this:

    private void Exit()
    {
        while(NavigationService.BackStack.Any())
             NavigationService.RemoveBackEntry();
    
        this.IsHitTestVisible = this.IsEnabled = false;
    
        if(this.ApplicationBar != null)
        {
             foreach(var item inApplicationBar.Buttons.OfType<ApplicationBarIconButton>())
             {
                  item.IsEnabled = false;
             }
        }
    }
    


    • Edited by yuchangjie Wednesday, January 2, 2013 8:54 AM
    Wednesday, January 2, 2013 8:40 AM