如何在Application_Launching事件中正确关闭应用程序

时间:2013-05-22 06:29:38

标签: windows-phone-7 windows-phone-8

如果网络不可用,我想关闭我的应用。

我在App.cs中检查网络:

private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                //close my app
            }
            else
            {
                 //continue to work
            }
        }

有更好的方法吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

只需添加对Microsoft.Xna.Framework.Game的引用我确定你可以使用这段代码退出,这样就可以了。如果你想显示消息框,你必须在主页面

我会做什么:

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
               MessageBoxResult m = MessageBox.Show(Sorry, no internet connection is available.do you want to exit the application , "Oops...", MessageBoxButton.OKCancel);
                if (m == MessageBoxResult.OK)
                {
                    var g = new Microsoft.Xna.Framework.Game();
                        g.Exit();
                }
            }           
        }

你应该提供一个“温和”的方式来结束

  

5.1.2 - 应用关闭

The app must handle exceptions raised by the any of the managed or native System API 
and not close unexpectedly. During the certification process, the app is monitored 
for unexpected closure. An app that closes unexpectedly fails certification. The app
must continue to run and remain responsive to user input after the exception is
handled.

for more information visit this link

答案 1 :(得分:1)

Application.Current.Terminate();