在contentpage XAMARIN表单中调用onresumo()方法

在contentpage XAMARIN表单中调用onresumo()方法,xamarin,xamarin.ios,xamarin.forms,Xamarin,Xamarin.ios,Xamarin.forms,我在工作中遇到了以下问题 我在iOS中的声明。我需要在用户返回后台应用程序时激活一个命令。在我的ContentPage上,我找不到识别退货的方法。在Android中,它工作正常,只有在iOS中,我在显示我返回的内容中找不到任何函数 protected override void OnAppearing() { Debug.WriteLine("OnAppearing()"); base.OnAppearing(); } protecte

我在工作中遇到了以下问题 我在iOS中的声明。我需要在用户返回后台应用程序时激活一个命令。在我的
ContentPage
上,我找不到识别退货的方法。在Android中,它工作正常,只有在iOS中,我在显示我返回的内容中找不到任何函数

 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();
    }

我尝试了这三个选项,但没有结果

正如josemigallas所说,您正在寻找
App.OnResume()
。您可以轻松订阅和取消订阅
MessagingCenter
事件,该事件可以从您的
App.OnResume()
方法发送,以触发订阅的每个
ContentPage
中的操作(不要忘了取消订阅
ContentPage.OnDisappearing()中的事件,以防止内存泄漏):

ContentPage
s中:

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

    MessagingCenter.Subscribe<App>(this, "SpecialOnResumeKeyValue", app => {
            //Do something
    };
}

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

    MessagingCenter.Unsubscribe<App>(this, "SpecialOnResumeKeyValue"); //VERY IMPORTANT
}

我通常是这样做的,这样我就不必为订阅费心了:

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();
}

也许这就是你要找的?Onresume在我的App.xaml.cs中被正确调用,要使用我的应用程序,我必须在contentpageYes中调用Onresume是的,对不起,没有意识到您正在使用XF。可能是愚蠢的建议,但您是否尝试过先调用基本方法p在没有结果的情况下,它继续在应用程序中仅强制转换onResume()。并且不调用任何内容为什么要为BindingContext执行单独的恢复?@DavidClarke因为平台提供了这样做的方法,所以接口也应该这样做。
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();
}