使用 WinRT 未引发暂停事件 [英] Suspending event not raising using WinRT

查看:13
本文介绍了使用 WinRT 未引发暂停事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having a problem with suspending event on Windows Phone 8.1 using WinRT, it does not fire. I don't know why. This is my code:

/// <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 没有运行.

I set a breakpoint on the line var deferral = e.SuspendingOperation.GetDeferral(); and debugged it with Visual Studio. Then I pressed the start button on my phone and ran another app and waited about 10 seconds. OnSuspending is not running.

有什么想法吗?

推荐答案

暂停事件不会在您调试时触发(但在正常运行应用程序时,它会在您离开应用程序后立即触发),因为它是在这个博客上也说过:

Suspending event won't fire while you are debugging (but while running your App normal, it will fire just after you navigate away from the App) as it is said also at this blog:

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

...you will wait forever for these to trigger, even though your app switches back and forth to the screen! The reason is simple: while an app is being debugged, Windows will not suspend it.

请注意,当 Suspending 事件出现问题时,这可能会导致一些奇怪的应用行为 - 例如,如果您在 Frame.Navigate 方法中传递了一些复杂的类然后你使用SuspensionManager.虽然调试您的应用程序会正常工作(不会暂停),但会在没有调试模式的情况下崩溃.

Note that this may lead to some weird app behavior, when there is something wrong in Suspending event - for example if you pass some complex class in Frame.Navigate method and you use SuspensionManager. While debugging your app will work just fine (no suspension), but will crash without debug mode.

要测试您的应用程序的行为方式,您必须调用暂停手动lt,打开(或设置可见)调试位置工具栏Visual Studio,你会在那里找到一个下拉Lifecyce events,选择那里Suspend,然后返回App - Resume.

To test how your App behaves, you will have to invoke the Suspending manuallt, open (or set visible) Debug location toolbar in Visual Studio, there you will find a dropdown Lifecyce events, choose there Suspend, and then to return the App - Resume.

这篇关于使用 WinRT 未引发暂停事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆