0
votes

I use Xamarin.Forms (ver 1.4)

Page hierarchy:

->NavigationPage

-->MasterDetailPage

How can disable sliding menu (open master page by gesture) in pushed screen?

Push method:

await ((NavigationPage)((MasterDetailPage)Application.Current.MainPage).Detail).PushAsync(page))

Sliding menu should work only on root page.

2
Hi BooMik, Did you get any solution of this problem? I am also facing same problem. Do you have any idea to fix it?anand

2 Answers

3
votes

MasterMenuPage holds a bool property called "IsGestureEnabled" which determines if the swipe in menu is available.

IsGestureEnabled = false //no swipe gesture

Here I can give you an easy implementation for a custom MasterDetailPage

public class MainMDPage : MasterDetailPage
    {
        public MainMDPage(Page masterPage, Page detailPage)
        {
            MasterBehavior = MasterBehavior.Popover;
            Master = masterPage;
            Detail = detailPage;
        }

        /// <summary>
        /// Pushes a page and disables the master menu.
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public async Task PushAsync(Page page)
        {
            if(Detail is NavigationPage navPage)
            {
                await navPage.PushAsync(page);
                IsGestureEnabled = false;
            }
        }

        /// <summary>
        /// Pops a page and enables the master menu if we get to the root page.
        /// </summary>
        /// <returns></returns>
        public async Task PopAsync()
        {
            if (Detail is NavigationPage navPage)
            {
                await navPage.PopAsync();

                if (navPage.Navigation.NavigationStack.Count == 1)  //if count == 1 -> is root page
                    IsGestureEnabled = true;
            }
        }
    }
0
votes

In my app, I have "RootPage" that inherits from MasterDetailPage. This is my MainPage for the Application. I have a "MenuPage" that inherits from ContentPage that creates the menu items. I set that to Master in "RootPage". Then I create a new NavigationPage, passing in page with a list view to the constructor. I then set the Detail to the NavigationPage. In the listview page, OnItemSelected I navigate with Navigation.PushAsync(detailsPage). Following this pattern, when the detailsPage loads, the "hamburger/sliding menu" is not there in iOS and instead I can only go back to the previous page (List view). Here are code examples:

public class RootPage : MasterDetailPage {
    MenuPage menuPage;

    public RootPage() {
        menuPage = new MenuPage();
        menuPage.Menu.ItemSelected += (sender, e) => 
                  NavigateTo(e.SelectedItem as Model.MenuItem);

        Master = menuPage;
        Detail = new NavigationPage(PlayerListPage());
    }
}

public class PlayerListPage : ContentPage {
    protected async void OnItemSelected(object sender, ItemTappedEventArgs e) {
        var item = e.Item as PlayerViewModel;
        var selected = new PlayerPage(item) {
            BindingContext = item
        };

        await Navigation.PushAsync(selected);
    }
}

public class MenuPage : ContentPage {
    public ListView Menu { get; set; }

    public MenuPage() {
        Icon = "menu.png";
        Title = "menu"; // The Title property must be set.
        var menuItems = new List<MenuItem> {
            new MenuItem { 
                Title = "Players", 
                IconSource = "people.png", 
                TargetType = typeof(PlayerListPage)
            },
        };

        Menu = new ListView { 
            ItemsSource = menuItems,
        };
    }
}

public class MenuItem {
    public string Title { get; set; }
    public string IconSource { get; set; }
    public Type TargetType { get; set; }
}

Is this what you are trying to achieve? If not, then you clarify what you mean by disable the sliding menu and provide more code samples.