当我的应用程序在后台并转到前台时,OnAppearing() 方法在 IOS 中不起作用,或者当手机锁定然后解锁并且应用程序在前台时,OnAppearing() 方法不会被调用,在 Android 上一切正常。 我在下面找到了这个指南,但仍然不起作用,我有最新版本的 xamarin 表单。
指南: https://kent-boogaart.com/blog/hacking-xamarin.forms-page.appearing-for-ios
谁能帮帮我?
最佳答案
如您所见,iOS 中的“生命周期”是不同的。一种有用的方法是使用应用程序生命周期并将它们绑定(bind)到 Page
事件(或命令,如果需要)。
在您的 Application 子类中添加几个公共(public) EventHandlers 并将它们绑定(bind)到 OnResume(如果需要,还绑定(bind)到 OnSleep)
public partial class App : Application
{
public EventHandler OnResumeHandler;
public EventHandler OnSleepHandler;
public App()
{
InitializeComponent();
MainPage = new MyPage();
}
protected override void OnSleep()
{
OnSleepHandler?.Invoke(null, new EventArgs());
}
protected override void OnResume()
{
OnResumeHandler?.Invoke(null, new EventArgs());
}
}
现在在您的 ContentPage
子类中,添加一个处理程序来跟踪该页面何时从后台返回,类似于“OnAppearing after OnPause”处理程序...
void Handle_OnResumeHandler(object sender, EventArgs e)
{
Console.WriteLine("OnPauseResumeWithPage");
}
protected override void OnAppearing()
{
(App.Current as App).OnResumeHandler += Handle_OnResumeHandler;
base.OnAppearing();
}
protected override void OnDisappearing()
{
(App.Current as App).OnResumeHandler -= Handle_OnResumeHandler;
base.OnDisappearing();
}
关于c# - IOS上的OnAppearing方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52186542/