Is there any possibility to programatically close Silverlight application on Windows Phone 7?
-
1Please read this: http://blog.jerrynixon.com/2011/11/mango-sample-exit-application.html – Jerry Nixon Feb 20 '12 at 19:39
-
This problem is solved with the post below showing the code to safely exit from the silverlight application. – Syed Umar Ahmed Apr 09 '12 at 15:20
-
Why don't you just allow user to press Back on the first page, so that the app can be quitted naturally ? – onmyway133 Nov 09 '12 at 10:50
14 Answers
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,692
- 2
- 28
- 55
-
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 02 '11 at 13:02
-
there is a way to programmatically close your app in a silverlight app, please refer to the below post – Syed Umar Ahmed Apr 09 '12 at 15:19
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.

- 5,612
- 1
- 21
- 23
-
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 02 '12 at 19:29
-
Well Luke i think you can go back to the OS . just use this code on the load of application page 1 and thn when u manually come to page 1 from page 3 . It will remove all the back entries is exists and on clicking the back button it will come out. i have done this in my app too. The only thing u need to come out to os on a back key press is the empty stack of navigationservice. I hope it helps u – Syed Umar Ahmed Feb 08 '12 at 05:18
-
1+1 - Not sure why more people aren't using this method. Works perfectly, and no throwing exceptions... – David Spence Feb 16 '12 at 23:36
-
1this is amazing, why is no one suggesting this, works like a charm for me. – Nico Apr 07 '12 at 16:34
-
1this 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 08 '12 at 10:13
-
1This 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 '12 at 08:27
-
There is no where written Automatically ! secondly you have to perform some action in order to quit from the app. Third if you want to do it automatically just call the back button through code... – Syed Umar Ahmed Apr 11 '12 at 12:26
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()
.

- 65,560
- 11
- 91
- 143
-
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. – Jeff Wilcox Sep 07 '10 at 16:21
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)
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.

- 31
- 1
Add a reference to Microsoft.Xna.Framework.Game, then call:
new Microsoft.Xna.Framework.Game().Exit();

- 125
- 2
- 6
-
why u are trying to add a XNA reference ? when its a silver light application – Syed Umar Ahmed Apr 09 '12 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 – Patrick Bounaix May 21 '12 at 13:10
-
Using this API for a silverlight application would make the app rejected on marketplace isn't it??? – Apoorva Jul 03 '12 at 07: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.) – Patrick Bounaix Jul 26 '12 at 23:23
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

- 3,901
- 2
- 24
- 50
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.

- 358
- 5
- 16
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
}

- 3,632
- 7
- 45
- 67
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;
}

- 359
- 8
- 11
-
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 '13 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();` – Karthik Krishna Baiju Dec 15 '13 at 04:21
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();

- 359
- 8
- 11
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.

- 165
- 2
- 14