【问题标题】:Suspending event not raising using WinRT未使用 WinRT 引发的挂起事件
【发布时间】:2014-07-28 23:30:23
【问题描述】:

我在使用 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 没有运行。

有什么想法吗?

【问题讨论】:

    标签: c# uwp windows-runtime windows-phone-8.1 win-universal-app


    【解决方案1】:

    在调试时不会触发暂停事件(但在正常运行应用程序时,它将在您离开应用程序后立即触发),正如 this blog 所说:

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

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

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

    【讨论】:

    • 能否提供任何证明链接?
    • @CAMOBAP 指向什么的证明链接?
    • 声明“在调试时暂停事件不会触发”的证明链接(如果存在)
    • @CAMOBAP 我不知道你为什么要我给你看证明链接——它不难找到,所以就在这里——MSDN blog——>'如何调试进程生命周期事件',它说:如果您想通过在每个处理程序中设置断点并按 F5 以在 Visual Studio 中启动调试器来调试前面部分中的代码,您将永远等待这些触发,即使您的应用程序在屏幕上来回切换!原因很简单:在调试应用时,Windows 不会暂停它。
    • 我花了好几个小时才找到这个小金块。 TBF 我真的不知道我在寻找什么——我遇到的症状(以及我在谷歌上搜索的内容)是“Windows 手机应用程序崩溃但不在调试模式下调试”——也许留下这条评论将有助于谷歌链接到这个伟大的为其他人回答谷歌搜索同样的事情。此答案不能解决问题,但可以帮助您识别问题。
    猜你喜欢
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 2013-03-01
    • 2011-06-05
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多