当我的应用程序位于后台并转到前台时,OnAppearing() 方法在 IOS 中不起作用,或者当手机锁定然后解锁并且应用程序位于前台时,不会调用 OnAppearing() 方法,在 Android 上一切正常。
我在下面找到了这个指南,但仍然不起作用,我有最新版本的 xamarin 表单。
Guide:
https://kent-boogaart.com/blog/hacking-xamarin.forms-page.appearing-for-ios https://kent-boogaart.com/blog/hacking-xamarin.forms-page.appearing-for-ios
谁能帮我?
正如您所看到的,iOS 中的“生命周期”是不同的。一种有用的方法是使用应用程序生命周期并将其绑定到Page
事件(或命令,如果需要)。
在您的应用程序子类中添加几个公共事件处理程序并将它们绑定到 OnResume (如果需要,还可以添加 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
子类,添加一个处理程序来跟踪该页面何时从后台返回,类似于“OnPause 后的 OnAppearing”处理程序...
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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)