【问题标题】:Monotouch: Controller brought to foreground - Notification?Monotouch:控制器被带到前台 - 通知?
【发布时间】:2011-07-26 15:57:14
【问题描述】:

单触式

当应用程序重新回到前台时,我需要激活的 ViewController 来了解这一点。

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

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

【问题讨论】:

    标签: notifications xamarin.ios foreground


    【解决方案1】:

    我发现了这个:

    把这个放在 ViewController 的 CTOR 中:

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

    然后创建这个方法来处理视图控制器中的事件。

    void EnterForeground (NSNotification notification)
    {
        Console.WriteLine("EnterForeground: " + notification.Name); 
    }
    

    注意:当您的应用程序被带到前台时,UIApplicationDelegate 将首先引发此问题,这是清除登录详细信息和安全相关检查等内容的好地方。

    public override void WillEnterForeground (UIApplication application)
    

    【讨论】:

      【解决方案2】:

      在我的 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);
      }
      

      【讨论】:

        猜你喜欢
        • 2012-01-28
        • 2018-11-11
        • 1970-01-01
        • 1970-01-01
        • 2019-02-22
        • 1970-01-01
        • 1970-01-01
        • 2012-04-09
        • 1970-01-01
        相关资源
        最近更新 更多