Events Windows应用商店应用程序/Metro关闭/挂起事件不起作用

Events Windows应用商店应用程序/Metro关闭/挂起事件不起作用,events,microsoft-metro,windows-store-apps,exit,Events,Microsoft Metro,Windows Store Apps,Exit,我有一个我正在制作的游戏,它涉及两个在线玩家。如果一方退出申请,应通知另一方。我正在使用onsuspend事件,但它不起作用。这是我的密码: public MainPage() { this.InitializeComponent(); Application.Current.Suspending += Current_Suspending; // and a bunch of other stuff } pri

我有一个我正在制作的游戏,它涉及两个在线玩家。如果一方退出申请,应通知另一方。我正在使用onsuspend事件,但它不起作用。这是我的密码:

    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函数可以工作-我已经对它进行了测试。

一旦您从挂起事件返回,应用程序就会挂起,因此在您的情况下,异步方法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();
}

有关应用程序生命周期的更多详细信息。

您必须在不使用async关键字的情况下完成此操作。