Windows Phone 7 close application
Asked Answered
B

14

27

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

Broadbrim answered 7/9, 2010 at 13:47 Comment(3)
Please read this: blog.jerrynixon.com/2011/11/mango-sample-exit-application.htmlWalley
This problem is solved with the post below showing the code to safely exit from the silverlight application.Katlaps
Why don't you just allow user to press Back on the first page, so that the app can be quitted naturally ?Collencollenchyma
K
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?

Klinges answered 7/9, 2010 at 14:19 Comment(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.Monoacid
there is a way to programmatically close your app in a silverlight app, please refer to the below postKatlaps
K
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.

Katlaps answered 15/12, 2011 at 13:4 Comment(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.Arid
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 uKatlaps
+1 - Not sure why more people aren't using this method. Works perfectly, and no throwing exceptions...Mccleary
this is amazing, why is no one suggesting this, works like a charm for me.Idolize
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.Servo
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"!Klinges
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...Katlaps
R
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().

Rampage answered 7/9, 2010 at 13:57 Comment(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.Katheleenkatherin
N
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)

Necessitarianism answered 25/10, 2010 at 23:48 Comment(0)
S
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.

Selection answered 3/12, 2012 at 21:3 Comment(0)
S
2

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

new Microsoft.Xna.Framework.Game().Exit();
Schlegel answered 27/3, 2012 at 17:8 Comment(4)
why u are trying to add a XNA reference ? when its a silver light applicationKatlaps
Aadding a reference doesn't impact the application, and if you need to exit the app cleanly, you have a 1-liner optionSchlegel
Using this API for a silverlight application would make the app rejected on marketplace isn't it???Trotyl
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.)Schlegel
J
1
private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
    while (NavigationService.CanGoBack)   
        NavigationService.RemoveBackEntry();
}

That works for me fine.

Jaehne answered 12/6, 2012 at 13:38 Comment(0)
S
1

You can close the app using this statement

Application.Current.Terminate();
Stoughton answered 23/10, 2013 at 7:34 Comment(1)
Question is about windows-phone-7, not windows-phone-8.Lumbering
N
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

Noaccount answered 14/1, 2014 at 13:24 Comment(0)
E
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.

Eccrinology answered 30/1, 2012 at 11:12 Comment(0)
M
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.

Mooring answered 24/10, 2012 at 12:37 Comment(0)
F
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
        }
Fendley answered 7/4, 2013 at 16:28 Comment(0)
S
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;
        }
Syllogistic answered 22/10, 2013 at 6:47 Comment(2)
That will not pass the certification procedure, because: "Apps are not permitted to exit themselves." That sounds stupid, but that's Microsoft policy.Lumbering
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();Syllogistic
S
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();
Syllogistic answered 24/11, 2013 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.