I am using the following method to set the pagenumber of my current page:
protected override void OnAppearing()
{
base.OnAppearing();
// Page number
_pageNumber = Navigation.NavigationStack.Count - 2;
}
Everything works fine, if I am navigating forward and pushing a new page onto the stack like this:
private async void btnContinue_Clicked(object sender, EventArgs e)
{
// Proceed to next view
await Navigation.PushAsync(new MainPage());
}
However, when I am navigating back from one page, the OnAppearing() method seems to be called before the previous page is popped. Thus, the _pageNumber variable will effectively be the one of the previous page. How do I work around that? I thought of placing it inside the constructor but the NavigationStack.Count always returns 0 in there.
My pages are dynamically programmed, therefore, I cannot hardcode the page number into the page because the same page is called but the values are changing based on the number of the page.
This will be an expected effect .
When the PopAsync method is invoked, the following events occur:
So if you want to get current count of NavigationStack , you could improve the answer like following :
In the demo , I used MessageingCenter to pass the current of NavigationStack to last page . And the label will display the current NavigationStack .
bool isFirstLoad = true;
protected override void OnAppearing()
{
base.OnAppearing();
if(isFirstLoad)
{
label.Text = Navigation.NavigationStack.Count.ToString();
isFirstLoad = false;
}
MessagingCenter.Subscribe<Object,int>(this, "pop", (arg, num) => {
label.Text = (num-1).ToString();
});
}
And when you call Pop
private async void Button_Clicked_1(object sender, EventArgs e)
{
MessagingCenter.Send<Object,int>(this, "pop", Navigation.NavigationStack.Count);
await Navigation.PopAsync();
}

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With