Xamarin.Forms - Master/detail page and navigation history issue

后端 未结 3 538
抹茶落季
抹茶落季 2020-12-31 12:50

I have an app which uses masterdetail page to show menu in all page. The navigation is happened in two way in my app. one from the menu and second way from Dashboard. so if

3条回答
  •  有刺的猬
    2020-12-31 13:31

    I was having same issue, Detail.Navigation.PushAsync(itemSelected) makes hamburger menu vanish and also creating another sub-class to retain seemed big work on code and performance. So, I decided to use my own stack datatype for Master detail page. It was bit tricky to keep track and code but working fine.

    Initialize it at the app load with current Detail page and for each item selected Push new page on top of stack.

    public partial class MyMasterDetailPage: MasterDetailPage
        {
            private Stack navigationStack = new Stack();
            public MyMasterDetailPage()
            {
                InitializeComponent();
                navigationStack.Push(Detail);
                try
                {
                    masterPage.listView.ItemSelected += OnItemSelected;
    
                }
                catch (Exception exc)
                {
    
                    System.Diagnostics.Debug.WriteLine(exc.Message);
                }
    
            }
    

    Override OnBackButtonPressed() in same Page code behind

            protected override bool OnBackButtonPressed()
            {
                try
                {
                    var lastPage = navigationStack.Pop();
                    if (lastPage.Equals(Detail))
                        lastPage = navigationStack.Pop();
    
                    Detail = (Page)lastPage;
                    IsPresented = false;
    
                   // to avoid app close when complete pop and new page is push on top of it 
                    if (navigationStack.Count == 0) 
                        navigationStack.Push(Detail);
                    return true;
                }
                catch (Exception)
                {
    
                    return base.OnBackButtonPressed();
                }
            }
    

提交回复
热议问题