检查当前是否显示ContentPage

时间:2018-02-05 18:59:09

标签: c# xamarin xamarin.forms

有没有办法判断当前是否显示了ContentPage?

我需要ContentPage中的事件处理程序内的代码来检查页面当前是否显示并采取相应的行动。

3 个答案:

答案 0 :(得分:3)

除了GBreen12的答案,你也可以这样做......

bool isShowingMyPage = Application.Current.MainPage is MyPage || (Application.Current.MainPage is NavigationPage navPage && navPage.CurrentPage is MyPage); //If you are using a MasterDetailPage or something else, then you would have to handle this differently

//I think the below will work for checking if any modals are showing over your page but you should definitely test it in different scenarios to make sure. I am not sure if the modal is immediately removed from ModalStack when it gets popped or not

isShowingMyPage = Application.Current.MainPage?.Navigation?.ModalStack?.LastOrDefault() == null;

答案 1 :(得分:2)

您可以覆盖在即将显示页面时调用的OnAppearing

Page lifecycle events in xamarin.forms

答案 2 :(得分:1)

您可以聆听NavigationPage的{​​{1}}和Pushed事件,如下所示:

Popped

当然,只有在页面被推入导航堆栈时才会调用它;如果每次页面出现在屏幕上时都需要调用它,那么请使用GBreen12或hvaughan3所说的内容。