0
votes

In our Xamarin Forms app we have multiple pages. When we navigate to an other page for the first time from the main navigation page it works as expected. When we then use the back button (in the app or the Android system button) and navigate again to the next page then it doesn't load. It changes the title but it shows an empty page.

It does not matter what page you use as first page to go to and what page is the second page to go to. It also doesn't work if the first and second page is the same page.

In the app we initialize the main page as followed:

MainPage = new NavigationPage(new MainPage());

The MainPage is an TabbedPage.

We just navigate like this:

if (Application.Current.MainPage is NavigationPage mainPage)
{
    await mainPage.Navigation.PushAsync(new DetailPage());
}

This worked before, but I think that during the update of Xamarin.Forms from 4.2.0.848062 to 4.3.0.947036 it broke. But I cannot find anything that could brake this is release notes.

== Edit ==

We saw some inconsistency in all the calls to navigate. So we created an helper class to do all the navigation. This now looks like this:

    public static class NavigationHelper
{
    public static void NavigateTo(Page page)
    {
        if (Application.Current.MainPage is NavigationPage mainPage)
        {
            Device.BeginInvokeOnMainThread(async () =>
            {
                await mainPage.Navigation.PushAsync(page);
            });
        }
    }
}

So we now only navigate like this:

NavigationHelper.NavigateTo(new DetailPage());

But this still doesn't fix the issue. The first time we get the correct page, the second navigation we get an empty screen. The title in the navigation bar is changing to the page title where we navigate to. But the rest keeps empty.

2
you can see the result of the navigation, var result = await mainPage.Navigation.PushAsync(new DetailPage()); then check the result and see if you have any kind of exception - Ricardo Dias Morais
When i try to do var result = await mainPage.Navigation.PushAsync(new DetailPage()); it gives the error: 'Cannot assign void to an implicitly-typed variable'. The Xamarin.Forms.INavigation.PushAsync(Page page) is an Task. - C. Molendijk
i'm sorry, my mistake, PushAsync() returns void, that's why you cant do that, i'm mixing prism navigation interface with this - Ricardo Dias Morais
That is an good idea. Just created an new Tabbed application, change the main page setting to MainPage = new NavigationPage(new MainPage());, added the navigation helper and used that to navigate. There are no problems. But so i still don't known why it doesn't work in our application. - C. Molendijk

2 Answers

0
votes

Are you remembering to use the "Navigation" of the (NavigationPage)Application.Current.MainPage; - and not the current pages Navigation?

0
votes

I've found it with some help. In the Output Window we found the message

System.ObjectDisposedException: Cannot access a disposed object.

This happened when we went back to the previous page and made it impossible to go to the next page. Turned out to be an bug in the view model. We where trying to add items to an observable list that was already disposed. Fixing this fixed the navigation issue.