Monotouch: 控制器被带到前台 - 通知?

4

MonoTouch

当应用程序被恢复到前台时,我需要活动的ViewController知道这一点。

是否有可以使用的事件或覆盖来确定视图是否被带到前台。

我找到了“WillEnterForegroundNotification”,但它是一个字符串,所以不确定如何使用它。

2个回答

8

我发现了这个:

将以下内容放入 ViewController 的构造函数中:

NSNotificationCenter.DefaultCenter.AddObserver (UIApplication.WillEnterForegroundNotification, 
    EnterForeground); 

创建一个方法来处理视图控制器中的事件。
void EnterForeground (NSNotification notification)
{
    Console.WriteLine("EnterForeground: " + notification.Name); 
}

注意:当您的应用程序被带到前台时,UIApplicationDelegate将首先收到此消息,这是清除登录详细信息和安全相关检查等内容的好地方。
public override void WillEnterForeground (UIApplication application)

0
在我的MonoTouch应用程序中,我有一个“添加”按钮,一旦点击后就会在当天的其余时间内被禁用。为了在应用程序变为活动状态时检查并启用该按钮,我在ViewController的构造函数中监视UIApplication.Notifications.ObserveDidBecomeActive
NSObject _didBecomeActiveNotification;
.
.
.
// and in constructor
_didBecomeActiveNotification = UIApplication.Notifications.ObserveDidBecomeActive((sender, args) => {
    SetRightBarButtonState();
});

然后我通过在ViewController中覆盖Dispose方法来实现。

protected override void Dispose (bool disposing) {
    if (disposing) {
        _didBecomeActiveNotification.Dispose();
    }
    base.Dispose (disposing);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接