Back Button does not appear in Navigationbar Xamarinforms

Asked

Viewed 220 times

1

I have a Hamburguer Menu with a Listview where in the click of the listview line I call this method to open a Modal, but the Navigationbar does not appear the Backbutton.

    public async Task OpenModalAsync(Page poPage, bool bNavigationPage = true)
    {

        if (bNavigationPage)
            await Application.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(poPage), true);
        else
            await Application.Current.MainPage.Navigation.PushModalAsync(poPage, true);
    }

I have tried to use the method, but without result

    NavigationPage.SetHasBackButton(this, true);

I found that in the Pushasync method it puts the Backbutton.

  • In Xamarinforms, navigation has two stacks:a for normal (pages added with the PushAsync) and the modal stack (pages added with PushModalAsync). When you add more than one modal page the 'back' button still does not appear?

  • @Diegorafaelsouza I did the test here and opening two modals does not appear the Backbutton, I think for modal should not even have.

  • Actually has yes, I tested before posting. It must be some page parameterization then. Only with this code is it hard to tell what’s going on, but surely what you want is possible.

  • @Diegorafaelsouza, thank you, I will test here to see what can be.

1 answer

1


In case in your menu harmburger you are on the "Root" screen it will not even appear.

I used in my project according to the selected item as Pushmodalasync, in this case it does not have the back button, but can use as Pushasync

You can create this way:

public interface INavigationService
    {
        Task PopModalAsync();
        Task NavigateToSubCategoria(Categoria categoria);
    }

public class NavigationService : INavigationService
    {
        public async Task PopModalAsync()
        {
            await NavigationHelper.PopModalAsync();
        }

        public async Task NavigateToSubCategoria(Categoria categoria)
        {
            await NavigationHelper.PushModalAsync(new SubCategoriasPage(categoria));
        }
    }

Codebehind :

private readonly INavigationService _navigationService;
        public CategoriasPage ()
        {
            InitializeComponent ();
            _navigationService = DependencyService.Get<INavigationService>();
        }

    void OnItemTapped(Object sender, ItemTappedEventArgs e)
            {
                var dataItem = (Categoria)(e.Item);

                _navigationService.NavigateToSubCategoria(dataItem);
            }

Xaml:

<ListView x:Name="ListaCategorias"
          ItemTapped="OnItemTapped">

    </ListView>
  • Its implementation is very similar to mine, but I did a test and the Hamburger Menu if it is not Root of Exception, I tried to open it in a Modal, also if I open a modal within another modal does not appear the backbutton, if I try to use the Pushasync that brings the Backbutton occurs to me the error "System.Invalidoperationexception: Pushasync is not supported globally on Android, Please use a Navigationpage. occurred" I created a new project to test and the same thing happens

  • For now, I put in Appplication.Current.Mainpage = new Navigationpage(new Menu()) I disabled in it the Toolbar to be able to use Pushasync, and it is coming with Backbutton, I think the option will be the same

  • You should then relocate your Root to Navigation. var mainPage = new Mainpage(); //this could be content page var rootPage = new Navigationpage(mainPage); App.Navigation = rootPage.Navigation;

  • That could also be what you said :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.