Windows Store应用程序/ Metro关闭/暂停事件不起作用

问题描述:

我有这个游戏,我正在制作涉及两个在线玩家。如果退出申请,应通知另一方。我正在使用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

一旦你从暂停事件返回应用程序暂停,那么在你的情况下,异步方法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