user1288906
user1288906

Reputation: 439

Menu disappears while navigating from one content page to another

I am building a Xamarin App, with a Master Detail Page Layout and When I navigate to a content page from another content aPage. The menu disappears on the content page.

I have three pages: MasterDetailPage.xaml.cs, ListItemsPage.xaml.cs and DepositsPage.xaml.cs

MasterDetailPage.xaml.cs

// Constructor.
public MainPage()
{
    InitializeComponent();
    Detail = new NavigationPage(new Login());
    IsPresented = false;
}

//Navigates to the content Page called ListItemsPage.xaml.cs

OnMenuItemSelected()
{
    Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(ListItemsPage));
}

On Click of a List Item on the ListItemsPage → It should navigate to another content page (Deposits.xaml) and the following is the code I use:

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

It navigates to the page but the menu is missing.

I would really appreciate it, if somebody could help me with this as I have been struggling with this the last couple days.

Upvotes: 0

Views: 628

Answers (2)

Nico
Nico

Reputation: 1

I had the same issue and the problem was that the title was missing.

After adding the title in to the content page everything worked like a charm…

Upvotes: 0

pinedax
pinedax

Reputation: 9346

When using MasterDetailPage navigation whenever you want to navigate but keeping the menu on the side you have to navigate changing the Detail part as you did when you replaced your Login with ListItemPagebut instead you are changing the whole Application page when you did Application.Current.MainPage

Something else I see in your code above is that you are not specifying the Master which is the part that should hold your Menu items.

Based on the Xamarin Documentation here you should define your MasterDetailsPage in this way:

public MainPage()
{
    var menuPage = new MenuPage();
    Master = menuPage;
    Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(ListItemsPage));

    menuPage.ListView.ItemSelected += OnMenuItemSelected
}

OnMenuItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    var item = e.SelectedItem as MasterPageItem;
    if (item != null) {
        Detail = new NavigationPage ((Page)Activator.CreateInstance (item.TargetType));
        masterPage.listView.SelectedItem = null;
        IsPresented = false;
    }    
}

But the above expects that your MenuPage will have a ListView where each item is a menu option containing all the information required to display the menu option and navigate.

public class MenuPage : ContentPage
{
    public ListView ListView { get { return listView; } }

    public MenuPage()
    {

        Icon = "hamburger.png";
        Title = "My great application";

        var masterPageItems = new List<MasterPageItem> ();
        masterPageItems.Add (new MasterPageItem {
        Title = "List Items",
        IconSource = "list_items.png",
        TargetType = typeof(ContactsPageCS)
        });
        masterPageItems.Add (new MasterPageItem {
        Title = "Deposit",
        IconSource = "deposit.png",
        TargetType = typeof(TodoListPageCS)
        });

        ..........
    }
}

If you follow the documentation for this you should be good. If still with doubts come back here.

Hope this helps.-

Upvotes: 2

Related Questions