Missing menu button on MasterDetailPage when assig

2019-08-21 06:53发布

问题:

I got a little App where I need to Swap from a ContentPage to a MasterDetailsPage.

MainPage = new NavigationPage(new MyContentPage());

Now if the login succeed I want to change the Root to MainPage = new NavigationPage(new MyMasterDetailpage());.

The MasterDetailsPage is shown, but the Button in the Navigation bar is missing then.

If I just use

Navigation.PushModalAsync(new MyMasterDetailpage());

I dont even get an Navigationbar, with

Navigation.PushModalAsync(new NavigationPage(new MyMasterDetailpage()));

The menu icon is missing again.

Anything I can do here?

Update

It seems that my problem is only on Android, didn't know that the new NavigationPage(new MyDetailView) hamburger only appears on android.

回答1:

NavigationMenu appears in MasterDetailPage when the Detail page is a NavigationPage. So rather than pushing MasterDetailsPage to Navigation stack, you may set it as MainPage. And then within MasterDetailPage, you need to add DetailPage within a NavigationPage. You can set the Icon for the menu with Icon property of MasterPage Here is an example below,

public class DashboardPage : MasterDetailPage
{
    DetailPage detailPage; 
    MenuPage  masterPage;
    NavigationPage detailNavigationPage;

    public DashboardPage ()
    {
        detailPage = new DetailPage ();
        detailNavigationPage=new NavigationPage(detailPage); // Navigation Page as parent for Detail Page.
        Detail = detailNavigationPage; 

        masterPage= new MenuPage(){Icon="ic_settings.png"}; // ic_settings.png willbe rendered as Menu Icon.
        Master = masterPage;

    }
}


回答2:

By setting the icon of your Master page you should get the image you supply as a button in the navigationbar.

Try someting like this in your MyMasterDetailpage

public class MyMasterDetailpage: MasterDetailPage
{
    public MyMasterDetailpage()
    {
        Detail = new NavigationPage (new Page());
        Master = new MenuPage () {
            Title = "Title",
            Icon = (Device.OS == TargetPlatform.iOS) ? "hamburgermenuicon.png" : null
        };
    }
}


回答3:

If you are pushing the MasterDetailPage using the PushModalAsync then navigation bar will not be shown as it will open the page as a Modal in fullscreen mode. If you push the page as :

await Navigation.PushAsync(new MyMasterDetailPage());

then u will get a back button on the navigation bar clicking it will move you backward to the page before. So your hamburger icon will not be visible.

The option is to set your MasterDetailPage as your MainPage

App.Current.MainPage = new MyMasterDetailPage();

If you want your Page which was added before the MasterDetailPage you can override the OnBackPressed() method in your android activity and push your page in the navigation stack and Pop your current MasterDetailpage.



回答4:

I had the same problem. But the solution was to make different the process. You wants to load, initially, the login view and then you show the view of the MasterDetail. I solved this as follows:

When you start the application, I create the view of the MasterDetail and immediately show the login view as a dialog, if the login is successful simply close the dialog. The code would look something like this:

public App()
{
    DependencyService.Register<IMessageService, MessageService>();
    DependencyService.Register<INavigationService,NavigationService>();

    InitializeComponent();

    MainPage = new NavigationPage(new MasterDetail());

    MercaFacil.App.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(new LoginView()));
}

When the login is successful simply called the PopModalAsync() method