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.
