Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

MasterDetailPage navigation history

$
0
0

I have created a MasterDetailPage and each time I select an item in my MenuPage the content is correctly displayed.

However, when I press the physical back button of my smartphone my application automatically close.

I tried to pass my NavigationPage to Navigation.PushAsync() but that throws an exception as the page already as a parent.

This is my current code which is working, but no navigation history:

MainActivity.cs:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    Xamarin.Forms.Forms.Init(this, bundle);
    SetPage(App.GetMainPage());
}

App.cs:

public class App
    {
        public static Page GetMainPage()
        {
            return new RootMasterDetailPage();
        }
    }

RootMasterDetailPage.cs:

class RootMasterDetailPage : MasterDetailPage
{
    OptionItem previousItem;

    public RootMasterDetailPage()
    {
        var masterPage = new MenuPage();
        masterPage.Menu.ItemSelected += (sender, e) => NavigateTo(e.SelectedItem as OptionItem);

        Master = masterPage;

        NavigateTo(masterPage.Menu.ItemsSource.Cast<OptionItem>().First());   
    }

    void NavigateTo(OptionItem option)
    {
        if (previousItem != null)
            previousItem.Selected = false;

        option.Selected = true;
        previousItem = option;

        var displayPage = PageForOption(option);

        Detail = new NavigationPage(displayPage); ;

        IsPresented = false;
    }

    Page PageForOption(OptionItem option)
    {
        if (option.Title == Constants.PageName.Home)
            return new HomePage();

        if (option.Title == Constants.PageName.Faq)
            return new FaqPage();

        if (option.Title == Constants.PageName.Contact)
            return new ContactPage();

        throw new InvalidOperationException();
    }
}

How can I handle the navigation history to allow my user to go back to previous detailled pages by using the smartphone back button?


Viewing all articles
Browse latest Browse all 58056

Trending Articles