Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call onresumo() method within a contentpage XAMARIN FORMS

I am having the following problem in my statement in iOS. I need to activate a command at the moment the user returns to the application when it is in the background. On my ContentPage I can not find a method to identify the return. In Android, it works correctly, only in iOS, I can not find any function in the content that shows me that I am returning

 protected override void OnAppearing()
    {
        Debug.WriteLine("OnAppearing()");
        base.OnAppearing();
    }

    protected override void OnDisappearing()
    {
        Debug.WriteLine("OnDisappearing");
        base.OnDisappearing();
    }

    protected override void OnBindingContextChanged()
    {
        Debug.WriteLine("OnBindingContextChanged");
        base.OnBindingContextChanged();
    }

I tried the three options but no results

like image 716
José Avatar asked Dec 14 '22 20:12

José


1 Answers

I usually do it this way so i don't have to bother with subscriptions:

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        MainPage = new MainPage();
    }

    protected override async void OnStart()
    {
        if (Application.Current.MainPage is IAppStateAware appStateAwarePage)
            await appStateAwarePage.OnStartApplicationAsync();
        if (Application.Current.MainPage.BindingContext is IAppStateAware appStateAwareVm)
            await appStateAwareVm.OnStartApplicationAsync();
        // Handle when your app starts
    }

    protected override async void OnSleep()
    {
        if (Application.Current.MainPage is IAppStateAware appStateAwarePage)
            await appStateAwarePage.OnSleepApplicationAsync();
        if (Application.Current.MainPage.BindingContext is IAppStateAware appStateAwareVm)
            await appStateAwareVm.OnSleepApplicationAsync();
        // Handle when your app sleeps
    }

    protected override async void OnResume()
    {
        if (Application.Current.MainPage is IAppStateAware appStateAwarePage)
            await appStateAwarePage.OnResumeApplicationAsync();
        if (Application.Current.MainPage.BindingContext is IAppStateAware appStateAwareVm)
            await appStateAwareVm.OnResumeApplicationAsync();
        // Handle when your app resumes
    }
}

public class YourContentPage : Page, IAppStateAware
{
    public Task OnResumeApplicationAsync()
    {
        throw new System.NotImplementedException();
    }

    public Task OnSleepApplicationAsync()
    {
        throw new System.NotImplementedException();
    }

    public Task OnStartApplicationAsync()
    {
        throw new System.NotImplementedException();
    }
}

public interface IAppStateAware
{
    Task OnResumeApplicationAsync();
    Task OnSleepApplicationAsync();
    Task OnStartApplicationAsync();
}
like image 184
Dbl Avatar answered Feb 26 '23 23:02

Dbl