暂停事件没有提高使用WinRT

我遇到了使用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(); } 

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

有任何想法吗?

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

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

请注意,这可能会导致一些奇怪的应用程序行为,当Suspending事件发生错误时 – 例如,如果您在Frame.Navigate方法中传递了一些复杂的类,并且使用了SuspensionManager 。 在调试你的应用程序时可以正常工作(不中断),但是会在没有调试模式的情况下崩溃。

要测试你的应用程序的行为,你将不得不调用 暂停手动,在Visual Studio中打开(或设置可见) 调试位置工具栏,在那里你会发现一个下拉Lifecyce事件,选择那里暂停 ,然后返回应用程序 – 简历

在这里输入图像描述