这是因为ViewIsAppearing
将始终在页面开始在屏幕上显示时调用。当您弹出第二页然后转到第一页时,第一页ViewIsAppearing
将再次触发。它导致死循环并禁止您的应用返回首页。
添加一个属性来避免这种情况:
bool isInitialized;
public FirstPageModel()
{
// ...
isInitialized = true;
}
protected async override void ViewIsAppearing(object sender, EventArgs e)
{
base.ViewIsAppearing(sender, e);
if (isInitialized)
{
await Task.Delay(100);
await CoreMethods.PushPageModel<SecondPageModel>();
isInitialized = false;
}
}
iOS可能会优化这个过程,但我还是建议你加上这个判断语句。
更新:
当您的应用程序到达主线程时调用它。
protected override void ViewIsAppearing(object sender, EventArgs e)
{
base.ViewIsAppearing(sender, e);
if (isInitialized)
{
Device.BeginInvokeOnMainThread(() =>
{
CoreMethods.PushPageModel<SecondPageModel>();
isInitialized = false;
});
}
}