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

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

我的问题是:

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

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

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

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

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

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

  NSObject observer = NSNotificationCenter.DefaultCenter.AddObserver( UIApplication.DidEnterBackgroundNotification, delegate(NSNotification ntf) { // Same as above }); 

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

 NSNotificationCenter.DefaultCenter.RemoveObserver(observer);