27

Is there any possibility to programatically close Silverlight application on Windows Phone 7?

3

14 Answers 14

14

If you write an XNA Game, you will have access to an explicit Exit() method. If you are writing traditional Silverlight project, then NO, there is no way to programatically close your app. See also Peter Torr's Blog entry on Exiting Silverlight Apps in Windows Phone 7. There he also mentions the option of throwing an unhandled exception, which IMO is a terrible programing style.

An option you may try, is using the WP7 Navigation Service to programatically navigate back out of the application. Not sure if that would work though. Why do you need to Exit?

2
  • FYI, CanGoBack is false when you are at the first page in your application. Trying to go back further throws an exception, which ultimately achieves your goal, albeit not very elegant.
    – Matt Ruwe
    Oct 2, 2011 at 13:02
  • there is a way to programmatically close your app in a silverlight app, please refer to the below post Apr 9, 2012 at 15:19
11

You can always call an exit by doing this at your landing page use this code on click of your application back button:

if (NavigationService.CanGoBack)
{
    while (NavigationService.RemoveBackEntry() != null)
    {
        NavigationService.RemoveBackEntry();
    }
}

This will remove back entries from the stack, and you will press a back button it will close the application without any exception.

7
  • Thanks. In my application pages flow like this 1 -> 2 -> 3, but the 2nd page is a wizard that gets you to 3rd page where I'm intercepting back button and manually navigating to page 1, skipping page 2. But because of the way Navigation Service works after manual page change I could not back out of an app back to the OS.
    – LukeP
    Jan 2, 2012 at 19:29
  • 1
    +1 - Not sure why more people aren't using this method. Works perfectly, and no throwing exceptions... Feb 16, 2012 at 23:36
  • 1
    this is amazing, why is no one suggesting this, works like a charm for me.
    – Nico
    Apr 7, 2012 at 16:34
  • 1
    this is the only clean way to make the phone exit the app after current page. Just execute this code on page Loaded event and next time user presses Back app will terminate without exception.
    – Roboblob
    Apr 8, 2012 at 10:13
  • 1
    This does not automatically close the app. The user has to manually press the back button in order to close the app. Insofar it does not answer the question (=> programatically close an app), which IMO implies "without user interaction"!
    – froeschli
    Apr 11, 2012 at 8:27
8

Short answer for Silverlight is No.
You should not provide a way to close the applicaiton. Closing the applicaiton should be the users choice and implemented by using the back button the appropriate number of times. This is also a marketplace requirement.

That said, a silverlight application will close if there is an unhandled exception. I have seen a few people try and create programmatic closing by throwing a custom error which is explicitly ignored in error handling. This can work but there is still the marketplace issue.

XNA applications can explictly call Exit().

1
  • Matt is correct here - from Silverlight you cannot, but you can from XNA. The link below to Peter's blog provides more information. Note that throwing a custom error is a violation of the marketplace policies and will result in the app being denied marketplace acceptance. Also, calling the XNA Exit method from a Silverlight app is not permitted. Sep 7, 2010 at 16:21
6

Some good info here already. Adding to this..

The platform is fully capable of managing closure of apps. The more apps don't provide an exit, the quicker users will become accustomed to not thinking about app house keeping, and let the platform manage it.

The user will just navigate their device using start, back, etc.

If the user wants out of the current app to go do something else quickly - easy - they just hit start.

.Exit(), whilst available for xna, really isn't required anymore either. There was a cert requirement during CTP that games had to provide an exit button. This is now gone.

Non game apps never had the need to implement this.

The more this topic's discussed (and it really has been given a good run around the block), the more the indicators to me suggest there is no need to code an exit.

Update: For those thinking of an unhandled exception as a suitable way of closing an app intentionally or letting the app close due to subpar operating conditions, I would recommend reviewing the comments concerning Application Certification Requirements in this answer. Is there a way to programmatically quit my App? (Windows Phone 7)

3

Here is another solution. If you have an error page that i.e. displays error to the end user you can use the

    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        base.OnBackKeyPress(e);
        e.Cancel = true;
    }

And you can instruct user to press start button to exit application.

2

Add a reference to Microsoft.Xna.Framework.Game, then call:

new Microsoft.Xna.Framework.Game().Exit();
4
  • why u are trying to add a XNA reference ? when its a silver light application Apr 9, 2012 at 15:20
  • Aadding a reference doesn't impact the application, and if you need to exit the app cleanly, you have a 1-liner option May 21, 2012 at 13:10
  • Using this API for a silverlight application would make the app rejected on marketplace isn't it???
    – Apoorva
    Jul 3, 2012 at 7:54
  • Not at all. Consider that there is a Visual studio project for Silverlight and XNA, so it should be no problem. (I have verified this with my own app and I know of others that do the same.) Jul 26, 2012 at 23:23
1
private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
    while (NavigationService.CanGoBack)   
        NavigationService.RemoveBackEntry();
}

That works for me fine.

1

You can close the app using this statement

Application.Current.Terminate();
1
  • 1
    Question is about windows-phone-7, not windows-phone-8.
    – Olter
    Oct 23, 2013 at 7:37
1

This worked perfectly on Windows phone 7

 System.Reflection.Assembly asmb = System.Reflection.Assembly.Load("Microsoft.Xna.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553");
            asmb = System.Reflection.Assembly.Load("Microsoft.Xna.Framework.Game, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553");
            Type type = asmb.GetType("Microsoft.Xna.Framework.Game");
            object obj = type.GetConstructor(new Type[] { }).Invoke(new object[] { });
            type.GetMethod("Exit").Invoke(obj, new object[] { });

Link - source

0

My 2 pence worth, reasons for an exit

1) there is no interent connection the first time it is run and it needs to create an account on a web service somewhere to run.

2) You need to force an upgrade for the user, again when tied to a web service, you may discover a bug in your app, or have web service changes that mean the user needs to be forced to upgrade, at that point you will want to inform the user that they must upgrade and then exit the app.

Currently in my app I am forced to take the user to a form that says "they" must exit, and if they click back they are again forced back to this page. not very nice.

0

In Silverlight, I throw an un-handled exception when I have to exit the application. I know that this isn't the graceful method to handle this but it is still the most convenient and easiest solution.

I know that according to the guidelines there shouldn't be any un-handled exceptions in the code but I write why I am explicitly throwing an un-handled exception in the Exception Request document at the time of submission.

Till now this method has always worked and never failed me.

0

Easiest way to do this is to add a reference to Microsoft.Xna.Framework.Game, then add using Microsoft.Xna.Framework.GamerServices; before namespace. Then we have a button in our Example.xaml with Click="quit_button". In out Example.xaml.cs we put this code inside our page-class:

private void quit_Click(object sender, EventArgs e)
        {
            new Microsoft.Xna.Framework.Game().Exit();
            //This will close our app
        }
0
 var buttonInfo = MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButton.OKCancel);
        if (buttonInfo == MessageBoxResult.OK)
        {
            if (NavigationService.CanGoBack)
            {
                while (NavigationService.RemoveBackEntry() != null)
                {
                       //
                }
            }
            e.Cancel = false;
        }
        else
        {
            //Stop page from navigating
            e.Cancel = true;
        }
2
  • That will not pass the certification procedure, because: "Apps are not permitted to exit themselves." That sounds stupid, but that's Microsoft policy.
    – Olter
    Oct 22, 2013 at 11:17
  • Navigate to App.xaml.cs in your solution explorer and add a static method to the App class public static void Exit() { App.Current.Terminate(); } so that you can call it anywhere from your application , as below App.Exit(); Dec 15, 2013 at 4:21
0

Navigate to App.xaml.cs in your solution explorer and add a static method to the App class

public static void Exit()
{
      App.Current.Terminate();
}

so that you can call it anywhere from your application , as below

App.Exit();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.