Search code examples
xamarinxamarin.androidxamarin.formsnavigationbarmaster-detail

How to navigate deeper inside a xamarin detailpage


I have a project with a MasterDetailPage as root page. When I navigate deeper inside the Detailpage, I have the problem, that the DetailPage navigation overrides my actionbar for the MasterPage. Can I have both in the actionbar, the burgermenuicon and the backbutton?

before navigation: before navigation after navigation: after navigation

public partial class MasterPage : MasterDetailPage
{
    public MasterPage()
    {

        Master = SetMasterContentPage();
        Detail = new NavigationPage(new TaxonomyOverviewPage());
    }

    ContentPage SetMasterContentPage()
    {
        var masterPage = new ContentPage { Title = "Test"};
        masterPage.Content = new StackLayout
        {
            Children = {
                new Label{Text="Label1"},
                new Label{Text="Label2"},
                new Label{Text="Label3"}
            }
        };

        return masterPage;
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
    }
}

Solution

  • Problem is solved.

    I used a public static MasterDetailPage and referenced it to the MainPage in App.cs. Now I can access the IsPresented property of the MasterDetailPage.

    public partial class App : Application
    {
        public static MasterPage masterdetail;
    
        public App()
        {
         InitializeComponent();
        }
    
        protected override void OnStart()
        {
          masterdetail = new MasterPage();
          Device.BeginInvokeOnMainThread(() => {
           MainPage = masterdetail;
           });
        }
    
    }
    

    Finally, I add a menuicon to the right side of the actionbar.

    protected override void OnAppearing()
        {
            base.OnAppearing();
            ToolbarItems.Add(new ToolbarItem("Menu", "menuicon.png", () => { App.masterdetail.IsPresented = true; }));
        }