如何通过MonoTouch通知我的申请被关闭/发送到后台?

Uwe*_*eim 5 c# iphone xamarin.ios

虽然我认为这是一个相当微不足道的问题,但我找不到任何答案.

我的问题是:

当我的应用程序被关闭或发送到后台时(通过用户点击主页按钮),有没有办法在MonoTouch iPhone应用程序中获取通知?

我认为WillTerminate覆盖对此有好处,但在调试器中,它永远不会被调用.

Dim*_*kos 8

当应用程序进入后台时,有两种方法可以获得通知:

一个.覆盖AppDelegate中的相应方法:

public override void DidEnterBackground(UIApplication application)
{
    // App entered background, do some light stuff here, 
    // there is not much time before getting suspended
}
Run Code Online (Sandbox Code Playgroud)

湾 通过NSNotificationCenter类添加通知观察器:

 NSObject observer = NSNotificationCenter.DefaultCenter.AddObserver(
    UIApplication.DidEnterBackgroundNotification, 
    delegate(NSNotification ntf) {

            // Same as above
    });
Run Code Online (Sandbox Code Playgroud)

您可以使用从AddObserver方法返回的NSObject对象在您不再需要它时删除观察者:

NSNotificationCenter.DefaultCenter.RemoveObserver(observer);
Run Code Online (Sandbox Code Playgroud)