【问题标题】:Windows Phone 8.1 - Exit() method not workingWindows Phone 8.1 - Exit() 方法不起作用
【发布时间】:2015-01-15 17:53:06
【问题描述】:

我的应用允许固定辅助磁贴,然后可用于启动应用。 我试图实现的行为是这样的:-

  • 如果在单击辅助磁贴时应用程序已经在运行,它会启动一个 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)

知道发生了什么以及如何让手机像模拟器一样工作吗?

【问题讨论】:

  • 我尝试在 Lumia 1520 上运行的应用程序的 OnLaunched 事件处理程序中调用 Exit() 方法,但应用程序确实退出了。
  • 我尝试调用 Exit() 方法并且它有效。似乎是 if (e.PreviousExecutionState == ApplicationExecutionState.NotRunning) 逻辑不起作用......奇怪。
  • 我很高兴我的评论有所帮助。
  • 是的,感谢您提供的信息。我仍然有这个问题。知道为什么 if 语句中的 Exit() 在模拟器上运行正常但在手机上运行正常吗?我很困惑。

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


【解决方案1】:

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

原来 Exit() 没问题,但我的 If 语句不正确。 在模拟器中,这行代码会捕获所有正在关闭的应用程序实例:

if (e.PreviousExecutionState == ApplicationExecutionState.NotRunning)

但在电话上,它也抛出了 ApplicationExecutionState.Terminated 和 ApplicationExecutionState.ClosedByUser

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

感谢您的帮助。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • Exit() 方法或 Application.Exit() 确实会退出应用程序。它在 8.1 模拟器中正常工作。如果我只使用 Exit() 自己,它也可以在电话上正常工作,但由于某种原因,当它在 if 语句中时不起作用。我想这意味着问题可能出在 if 语句上。
    猜你喜欢
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多