Xamarin GoBackAsync does not return to last page.

tim 120 Reputation points
2024-04-20T18:15:44.7833333+00:00

I have a tabbed main page that I can select other pages from.

one page has code to select a row and navigate from that content page to another content page to edit the selected row.

Checklist page

var navService = PageModelLocator.Resolve<NavigationService>();

        await navService.NavigateToAsync<TreeInfoPageModel>(checkListItem, true);
```/* tried true and false setting root  ( in nav service 

public async Task NavigateToAsync<TPageModelBase>(object navigationData = null, bool setRoot = false)

*/

**nav back code  ( i might assume that I'm resetting the stack with the VAR statement )**

```typescript
         var navService = PageModelLocator.Resolve<NavigationService>();

        await navService.GoBackAsync();
```// but will not compile without the var statement.

second issue is the tabs disappear when I navigate to the second page, but if the tabs return when I navigate back this wont be an issue. I do not need the tabs when I Navigate to the edit page.

Tia

Tim.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,620 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2024-04-22T03:34:00.8733333+00:00

    Hello,

    Xamarin support will end on May 1, 2024, it's recommended that you migrate your Xamarin app to .NET MAUI. Please see Xamarin official support policy .

    Firstly, please open the NavigationService.cs and find NavigateToAsync method. I change the code in the else part. I set the App.Current.MainPage =new NavigationPage( tabPage); When page is Tabbedpage, then change the MainPage is NavigationPage and navigated by navPage.Navigation.PushAsync like following code.

     public async Task NavigateToAsync<TPageModelBase>(object navigationData = null, bool setRoot = false)
            where TPageModelBase : PageModelBase
        {
            var page = PageModelLocator.CreatePageFor(typeof(TPageModelBase));
    
    
           if (setRoot)
            {
                if (page is TabbedPage tabbedPage)
                {
                    App.Current.MainPage = tabbedPage;
                }
                else
                {
                    App.Current.MainPage = new NavigationPage(page);
                }
                
            }
            else
            {
                if (page is TabbedPage tabPage)
                {
    
                   //change here, but you need to hide the navigationbar by the   android:TabbedPage.ToolbarPlacement="Bottom" in the MenuPage.xaml
                    App.Current.MainPage =new NavigationPage( tabPage);
                }
                //change here, detect current Main page is Navigation page
                else if  (App.Current.MainPage is NavigationPage navPage)
                {
                    //change here. navigate the detailed page
                    await navPage.Navigation.PushAsync(page);
                }
                else
                {
                    App.Current.MainPage = new NavigationPage(page);
                }
            }
    
    
           if (page.BindingContext is PageModelBase pmBase)
            {
                await pmBase.InitializeAsync(navigationData);
            }         
          
        }
    

    Next, please open the MenuPage.xaml add NavigationPage.HasNavigationBar="False" to hide the navigationbar.

    <TabbedPage
    ...
               NavigationPage.HasNavigationBar="False">
    

    And open the TreeInfoPage.xaml. please remove the android:TabbedPage.ToolbarPlacement="Bottom" in the <ContentPage>, after navgationg, you can see the back icon.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful