Windows Phone 8.1 - Exit()方法无法正常工作

时间:2015-01-14 12:09:58

标签: c# windows-phone-8.1 windows-phone-8-emulator

我的应用程序允许固定辅助磁贴,然后可以使用它来启动应用。 我想要实现的行为是: -

  • 如果在点击辅助磁贴时应用程序已在运行,则会启动URI并使应用程序保持运行
  • 如果在单击辅助磁贴时应用程序未运行,则会启动URI,然后调用Exit()方法退出应用程序(以便用户在应用切换视图中看不到额外的应用程序)。

我在app.xaml.cs中的OnLaunched()中使用的代码,如下所示。

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
#if DEBUG
        if (System.Diagnostics.Debugger.IsAttached)
        {
            this.DebugSettings.EnableFrameRateCounter = true;
        }
#endif

       // Check to see if app has been launched from a secondary tile 
       if (e.Kind==ActivationKind.Launch && e.TileId != "App")
       {
           //If so then launch Uri passed from tile
           var uri = new Uri(e.Arguments);
           Windows.System.Launcher.LaunchUriAsync(uri);

           //If launched from a secondary tile, then close the app if it wasn't already running
           if (e.PreviousExecutionState == ApplicationExecutionState.NotRunning)
           {
               Exit();
           }


       }

        Frame rootFrame = Window.Current.Content as Frame;

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (rootFrame == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();

            // TODO: change this value to a cache size that is appropriate for your application
            rootFrame.CacheSize = 1;

            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                // TODO: Load state from previously suspended application
            }

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }

        if (rootFrame.Content == null)
        {
            // Removes the turnstile navigation for startup.
            if (rootFrame.ContentTransitions != null)
            {
                this.transitions = new TransitionCollection();
                foreach (var c in rootFrame.ContentTransitions)
                {
                    this.transitions.Add(c);
                }
            }

            rootFrame.ContentTransitions = null;
            rootFrame.Navigated += this.RootFrame_FirstNavigated;

            // When the navigation stack isn't restored navigate to the first page,
            // configuring the new page by passing required information as a navigation
            // parameter
            if (!rootFrame.Navigate(typeof(SignInPage), e.Arguments))
            {
                throw new Exception("Failed to create initial page");
            }
        }

        // Ensure the current window is active
        Window.Current.Activate();
    }

当我在模拟器中运行它时,它完全按照要求运行。 但是,当我在我的设备(诺基亚925)上运行时,应用程序在从辅助磁贴启动时不会退出,因此在任务切换器视图中总会有一个应用程序窗口,即使它之前没有运行。

编辑我尝试在OnLaunched()上调用它自己的Exit()方法并正确退出。我只能假设这是'if'检查,看看应用程序是否正在运行,但是没有调用Exit()

 if (e.PreviousExecutionState == ApplicationExecutionState.NotRunning)

有什么想法,以及如何让手机像模拟器那样运作?

2 个答案:

答案 0 :(得分:1)

好的,所以这不是我认为的问题。

事实证明Exit()很好,但我的If语句不正确。 在模拟器中,此行捕获正在关闭的应用程序的所有实例:

if (e.PreviousExecutionState == ApplicationExecutionState.NotRunning)

但在电话中,它还抛出了ApplicationExecutionState.Terminated和ApplicationExecutionState.ClosedByUser

我不确定为什么在模拟器中手动关闭应用仍会返回NotRunning而不是ClosedByUser,但这就是问题所在。

感谢您的帮助。

答案 1 :(得分:-1)

如果您在退出应用程序时默认使用Windows运行时为Windows Phone 8.1开发应用程序,该应用程序将进入bachground。 据我所知,它无法关闭应用程序。