简体   繁体   中英

How to navigate away from MasterDetailPage but not reset the navigation stack

I finally found away (since PushAsync() doesn't work globally) how to navigate to a next page from a MasterDetailPage like so:

Application.Current.MainPage = new NavigationPage(new Screen_Profile());

This opens a new page, but then I cannot go back to the previous page, but this is resetting the navigation stack.

How can I navigate away but still have the option of returning to MasterDetail page navigation?

you can try using

 Navigation.PushAsync(new Screen_Profile());

try to read more on this link https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical

Here is the reference about the navigation with masterDetailpage

In your App.cs you would have something similar to this code (apologies if it doesn't compile, I am coding this off the top of my head).

public static NavigationPage NavPage = null;

public App()
{
    var _navPage = new NavigationPage(new PageOne());

    var _masterPage = new MasterDetailpage();
    _masterPage.Detail = NavPage;
    _masterPage.Master = (your master / menu page);

    MainPage = _masterPage;

}

Then in your code you can reference

await App.NavPage.PushAsync(new PageTwo());

That gets you functional. However you don't want to access the NavigationPage this way. You can use DependencyInjection to create a NavigationService.

Something like

public class NavigationService: INavigationService
{

      public NavigationService(NavigationPage navPage)
      {
            // Keep local copy of navPage
      }

      public async Task NavigateTo(Page page)
      {

            await _navPage.PushAsync(page);

      }

      public async Task GoBack()
      {

            await _navPage.PopAsync();

      }

}

You should use like this Pattern to navigate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM