2013-04-28 66 views
1

我有這個遊戲,我正在製作涉及兩個在線玩家。如果退出申請,應通知另一方。我正在使用onsuspend事件,但它不起作用。這裏是我的代碼:Windows Store應用程序/ Metro關閉/暫停事件不起作用

public MainPage() 
    { 
     this.InitializeComponent(); 

     Application.Current.Suspending += Current_Suspending; 
     // and a bunch of other stuff 
    } 

    private async void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e) 
    { 
     await game.leaveGame(); 
    } 

任何幫助將不勝感激。我100%肯定game.leaveGame()函數的工作原理 - 我測試過了。

+0

您必須在不使用* async *關鍵字的情況下進行此項工作。 – 2013-04-28 20:56:15

回答

0

一旦你從暫停事件返回應用程序暫停,那麼在你的情況下,異步方法game.leaveGame()將不會完成。 嘗試更改您的代碼(假設game.leaveGame是同步的方法):

public MainPage() 
{ 
    this.InitializeComponent(); 

    Application.Current.Suspending += Current_Suspending; 
    // and a bunch of other stuff 
} 

private void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e) 
{ 
    game.leaveGame(); 
} 

有關應用程序生命週期here更多細節。

+0

謝謝,它現在可行! – 2013-05-01 20:26:08