4

I am rewriting an app that I wrote in Xamarin.iOS into Xamarin.Forms and I used to have some codes to execute on DidEnterBackground and WillEnterForeground. Now I can't find the equivalent method in Xamarin.Forms. I've tried mainPage.Appearing and mainPage.Disappearing in my App class but they seem to be different from what I am trying to achieve. Anyone?

dmc
  • 807
  • 2
  • 10
  • 25

1 Answers1

4

I do not believe that functionality is in Xamarin.Forms yet. You could install Xamarin.Forms.Labs (available on NuGet) and inherit your app delegate from XFormsApplicationDelegate. You can look at a sample app delegate on the GitHub sources.

public partial class AppDelegate : XFormsApplicationDelegate
{

You would then need to register the IXFormsApp interface with a DI container:

    private void SetIoc()
    {
        var resolverContainer = new SimpleContainer();

        var app = new XFormsAppiOS();
        app.Init(this);

        resolverContainer.Register<IXFormsApp>(app);

        Resolver.SetResolver(resolverContainer.GetResolver());
    }

Once the application has been registered on the DI container you would use it on your shared/PCL code through the Resolver. You would be subscribing to the Resumed and Suspended events. Sample here.

        var app = Resolver.Resolve<IXFormsApp>();
        if (app == null)
        {
            return;
        }

        app.Closing += (o, e) => Debug.WriteLine("Application Closing");
        app.Error += (o, e) => Debug.WriteLine("Application Error");
        app.Initialize += (o, e) => Debug.WriteLine("Application Initialized");
        app.Resumed += (o, e) => Debug.WriteLine("Application Resumed");
        app.Rotation += (o, e) => Debug.WriteLine("Application Rotated");
        app.Startup += (o, e) => Debug.WriteLine("Application Startup");
        app.Suspended += (o, e) => Debug.WriteLine("Application Suspended");
SKall
  • 5,234
  • 1
  • 16
  • 25