Xamarin Forms Shell Navigate to current page

varyamereonvaryamereon ✭✭USMember ✭✭

Hi there,

Thought I would ask this here as I'm not sure if it's a feature or a bug. I have an application which I am creating with the new Shell structure. I am using the built in search handler which is intended to be used app wide to search for certain items. Executing a query then navigates to the item page with the search parameter. The thing is the search box is available still on the item page for the user to search again. In the past I have been able just to navigate to a new instance of the current page and it is added to the stack, but this doesn't seem to be possible with Shell's GoToAsync. If the route requested is the same as the currently displayed Shell page the request is ignored. Because Shell is handling the navigation stack I can't think of another way around this.

Does anybody from the Xamarin team know if this is an intended restriction? Otherwise I can file a bug on GitHub.

Many thanks

Will

Best Answer

Answers

  • MikilllMikilll ✭✭✭ Member ✭✭✭

    Could you attach some example code?

  • varyamereonvaryamereon ✭✭ USMember ✭✭

    Hi @Mikilll ,

    I have published a sample here. It uses a very similar structure to my app. The sample contains two pages, a books page and an about page. The user can search for a book using the Shell search bar and the app navigates to the book page and displays the book (ok the search feature needs a bit of debugging in the sample, but hey I tried lol).

    If the search is executed from the about page then the books page is navigated to successfully. If the books page is open already no navigation is carried out by GoToAsync. This is a pattern for search that I have used in the past before Shell that has worked fine but it does not work here.

    Many thanks

    Will

  • LandLuLandLu Xamurai Member, Xamarin Team Xamurai

    @varyamereon This is because you are using absolute routes: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#absolute-routes
    It will change the hierarchy and bring your top page back to the books. If your top page has been book then it will do nothing.
    Try to register your routes in the code behind like:

    public AppShell()
    {
        InitializeComponent();
    
        Routing.RegisterRoute("books", typeof(BookPage));
    }
    

    Then use var state = Shell.Current.CurrentState; to get the current route. Add the next route to finish the navigating:

    MainThread.BeginInvokeOnMainThread(async () =>
    {
        var state = Shell.Current.CurrentState;
        await Shell.Current.GoToAsync($"{state.Location}/books?name={found.Name}");
    });
    

    Please check the attachment to see the detailed code.

  • varyamereonvaryamereon ✭✭ USMember ✭✭

    Hi @LandLu ,

    Thanks for your reply. That is working better, the only trouble in this case is that after the navigation takes place the hamburger menu is replaced with the back arrow. I'm not quite sure how to do it so the hamburger button stays there in the same way as if I had navigated from the about page.

    Many thanks

    Will

  • LandLuLandLu Xamurai Member, Xamarin Team Xamurai

    @varyamereon The hamburger button only appears when each flyout item displays the root page.
    If it has navigated to another page the left side tool bar item changes to the left arrow so that you can consume it to pop back.

  • varyamereonvaryamereon ✭✭ USMember ✭✭

    Thanks @LandLu . I get the idea but that's not what I wanted in my case. I'd like it to be a new root page every time otherwise it's annoying for the user, if they perform 5 searches for example they would have to then go all the way back through the stack to get back to the menu.

    Many thanks

    Will

  • varyamereonvaryamereon ✭✭ USMember ✭✭

    Thanks @LandLu , I figured I may have to do something like that.

    Many thanks for your help.

    Will

Sign In or Register to comment.