Windows Phone 7 close application

2019-01-04 13:49发布

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

14条回答
仙女界的扛把子
2楼-- · 2019-01-04 14:12
 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;
        }
查看更多
\"骚年 ilove
3楼-- · 2019-01-04 14:15

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.

查看更多
欢心
4楼-- · 2019-01-04 14:19

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

查看更多
来,给爷笑一个
5楼-- · 2019-01-04 14:22

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?

查看更多
ら.Afraid
6楼-- · 2019-01-04 14:23

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.

查看更多
欢心
7楼-- · 2019-01-04 14:25

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();
查看更多
登录 后发表回答