挂起事件不使用WinRT引发

Ult*_*pon 35 c# windows-runtime windows-phone-8.1 win-universal-app uwp

我在使用WinRT在Windows Phone 8.1上暂停事件时遇到问题,它不会触发.我不知道为什么.这是我的代码:

/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
    InitializeComponent();

    Suspending += OnSuspending;
#if DEBUG
    this.displayRequest = new DisplayRequest();
#endif
}

/// <summary>
/// Invoked when application execution is being suspended. Application state is saved
/// without knowing whether the application will be terminated or resumed with the contents
/// of memory still intact.
/// </summary>
/// <param name="sender">
/// The source of the suspend request.
/// </param>
/// <param name="e">
/// Details about the suspend request.
/// </param>
private void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();
    deferral.Complete();
}
Run Code Online (Sandbox Code Playgroud)

我在该行上设置了一个断点var deferral = e.SuspendingOperation.GetDeferral();并使用Visual Studio对其进行了调试.然后我按下手机上的开始按钮并运行另一个应用程序,等待大约10秒钟.OnSuspending没有运行.

有任何想法吗?

Rom*_*asz 64

在您调试时,暂停事件不会触发(但是在​​运行您的应用程序正常时,它会在您离开应用程序后立即触发),正如在此博客中所述:

...即使你的应用程序来回切换屏幕,你也会永远等待这些触发!原因很简单:在调试应用程序时,Windows不会暂停它.

请注意,当Suspending事件中出现错误时,这可能会导致一些奇怪的应用程序行为- 例如,如果您在Frame.Navigate方法中传递了一些复杂的类,并且您使用了SuspensionManager.虽然调试你的应用程序会很好(没有暂停),但没有调试模式会崩溃.

为了测试你的应用程序的行为,你将不得不调用挂起 manuallt,打开(或设置可见)调试位置工具栏在Visual Studio中,你会发现一个下拉Lifecyce活动,选择有Suspend,然后再返回应用程序- Resume.

在此输入图像描述

  • @CAMOBAP我不确定你为什么要让我给你看证明链接 - 这里不难找到 - [MSDN博客](http://blogs.msdn.com/b/mspfe/archive /2013/06/17/suspend-and-resume-in-winrt.aspx) - >'如何调试进程生命周期事件',它说:*如果你想通过设置一个断点来调试前面几节中的代码每个处理程序并按F5启动Visual Studio中的调试器,即使您的应用程序来回切换到屏幕,您也会等待它们永远触发!原因很简单:在调试应用程序时,Windows不会暂停它.* (7认同)
  • 我花了几个小时才找到这个小小的金块.TBF我真的不知道我在寻找什么 - 我的症状(以及我在谷歌搜索)是​​"Windows手机应用程序崩溃,但没有在调试模式调试" - 也许留下这个评论将有助于谷歌链接到这个伟大的回答其他人谷歌搜索同样的事情.此答案无法解决问题,但可帮助您确定问题. (4认同)
  • @CAMOBAP一个证据链接到什么? (3认同)