【问题标题】:xamarin.forms detect ios app entering foreground from backgroundxamarin.forms 检测 ios 应用程序从后台进入前台
【发布时间】:2019-07-16 10:39:53
【问题描述】:

我正在Xamarin.Forms 共享项目中创建应用程序。我想检测来自后台的应用程序

目前,我在AppDelegate中实现了OnActivated方法,但是当我们在iphone中打开控制中心时也会调用这个方法。当应用程序从后台进入前台时,我希望我的应用程序重新连接到我的服务器并相应地显示消息,但是当我向上滑动并打开控制中心时也会显示此消息。

在原生Xamarin.iOS应用中,有一个方法WillEnterForegroundNotification,但是这里没有这个方法。

是否有任何方法只在应用来自后台时才被调用?

【问题讨论】:

  • 您是否尝试过在您的AppDelegate 中覆盖public override void WillEnterForeground(UIApplication uiApplication)

标签: xamarin.forms xamarin.ios


【解决方案1】:

App.xaml.cs

protected override void OnResume()

【讨论】:

  • 感谢您的评论,OnResume 中也会发生同样的行为。即使我尝试更改 wifi 或播放一些音乐(控制中心)也会调用它
  • 我在“OnResume”期间使用 Xamarin.Forms.MessagingCenter 广播一个事件,并在我需要处理该事件的任何地方订阅。
【解决方案2】:

在本机 Xamarin.iOS 应用程序中,有一个方法 WillEnterForegroundNotification 但此方法在此处不可用。

您可以在 Xamarin.forms 项目中访问 WillEnterForegroundNotification 方法,只需在 AppDelegate.cs 中覆盖它:

public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    //
    // This method is invoked when the application has loaded and is ready to run. In this 
    // method you should instantiate the window, load the UI into it and then make the window
    // visible.
    //
    // You have 17 seconds to return from this method, or iOS will terminate your application.
    //
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App());

        return base.FinishedLaunching(app, options);
    }

    public override void WillEnterForeground(UIApplication uiApplication)
    {   
        //handle your event here 
        base.WillEnterForeground(uiApplication);
    }
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 2019-01-28
    相关资源
    最近更新 更多