36

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実行されていません。

何か案は?

4

1 に答える 1

65

このブログでも述べられているように、デバッグ中はサスペンド イベントは発生しません (ただし、アプリを正常に実行している間は、アプリから離れた直後にイベントが発生します) 。

...アプリが画面を行ったり来たりしても、これらがトリガーされるのを永遠に待つことになります! 理由は簡単です。アプリがデバッグされている間、Windows はアプリを中断しません。

Suspendingイベントに何か問題がある場合、たとえば、 Frame.Navigateメソッドで複雑なクラスを渡し、SuspensionManagerを使用する場合など、アプリの奇妙な動作が発生する可能性があることに注意してください。アプリのデバッグ中は正常に動作しますが (一時停止はありません)、デバッグ モードを使用しないとクラッシュします。

アプリの動作をテストするには、一時停止中のマニュアルを呼び出し、 Visual Studio で [デバッグの場所] ツールバーを開く (または表示するように設定する)必要があります。ドロップダウンの [ Lifecyce events] を見つけて、そこを選択し、アプリを返します - .SuspendResume

ここに画像の説明を入力

于 2014-06-08T06:51:54.487 に答える