0

I have created a Master Detail Page

I am loading a list of items into the 'detail' frame / window

I want to replace the contents of that page with a template / view which never has any reason to exist as an item in the Menu Items

I have tried replacing MainPage and Navigation which load the page but you lose the Master Detail context - the menu

Please can someone tell me what I call in order to replace the current page with one of my choice while staying within the context of Master Detail?

This does not work, for example - it removes the MasterDetail menu

 Navigation.PushAsync(new Arcade.Index());

I have created the MasterDetailPage by pretty much letting Visual Studio generate it. I set it after a successful login, like so:

            var welcome = new Pages.Welcome();


            Application.Current.MainPage = welcome;

This is an excerpt of the XAML for Welcome

 <MasterDetailPage.Master>
    <pages:WelcomeMaster x:Name="MasterPage" />
    </MasterDetailPage.Master>
    <MasterDetailPage.Detail>
 <NavigationPage>
    <x:Arguments>
    <pages:Index />
   </x:Arguments>
 </NavigationPage>

I've added this to the code behind for Welcome

        InitializeComponent();
        MasterPage.ListView.ItemSelected += ListView_ItemSelected;

        this.Detail = new NavigationPage(new Arcade.Index());

In spite of all that, when I call this later, the MasterDetail menu disappears

    ((MasterDetailPage)Application.Current.MainPage).Detail = new Arcade.Index();
Journeyman1234
  • 547
  • 4
  • 18
  • Please provide a [minimal reproducable example](https://stackoverflow.com/help/minimal-reproducible-example) – Mouse On Mars Aug 28 '19 at 19:41
  • set the Detail property of the MasterDetailPage – Jason Aug 28 '19 at 19:48
  • I've read that a few times but there is no way of accessing that from the view I've loaded. There is no relationship between the page I've loaded to the Master Detail that I can find – Journeyman1234 Aug 28 '19 at 19:49
  • either use Navigation to navigate to a new page within the Detail frame, OR reference the current MasterDetail from the App.Current.MainPage, OR just manually pass a reference to your MasterDetail when you create the detail page – Jason Aug 28 '19 at 19:54
  • App.Current.MainPage replaces the entire window, I cannot see any way of accessing the existing MasterDetail via that method. Navigation I gave in my example, that replaces the entire window. I don't understand the third method? – Journeyman1234 Aug 28 '19 at 19:57
  • You can check my answer out [here](https://stackoverflow.com/a/49175351/7462031) where you can find everything related to the MasterDetail setup – FreakyAli Aug 29 '19 at 04:54

2 Answers2

1

In you App.xaml.cs add static method then you can use it to navigate in you code

    public static void SetDatailPage(Page page)
    {
        if (App.Current.MainPage is MasterDetailPage)
        {
            var masterPage = (MasterDetailPage)App.Current.MainPage;
            masterPage.Detail = new NavigationPage(page);
        }
    }

And use it like this

App.SetDatailPage(new YourPageYouWannaNavigateTo());
Adlorem
  • 1,457
  • 1
  • 11
  • 10
0

1 - use App.Current.Mainpage

var md = (MasterDetailPage)App.Current.MainPage;
md.Detail = new MyPage();

2 - use navigation

when you initially create your detail, wrap it in a NavigationPage

this.Detail = new NavigaitionPage(new Page1());

then later, Page1 can navigate to Page2 within the Detail pane

Navigation.PushAsync(new Page2());

3 - explicitly pass a reference to the MasterDetailPage

// when you create Page1, pass a reference to the MasterDetailPage
// you will also need to modify Page1's constructor
this.Detail = new Page1(this);
Jason
  • 86,222
  • 15
  • 131
  • 146
  • I really appreciate the response but I appear to be missing something fundamental. For example, this variant of what you've put above still replaces the entire window with the page I want to load - so I lose the menu. None of these options lets me load a page that keeps the MasterDetail menu. ((MasterDetailPage)Application.Current.MainPage).Detail = (new Arcade.Index()); – Journeyman1234 Aug 28 '19 at 20:29
  • Then you're doing something wrong. If you set MainPage to be a MasterDetailPage, and then you later update the **Detail** property, that will only replace the Detail, not the Master. – Jason Aug 28 '19 at 20:31
  • you may be losing the toolbar button that triggers the master page appearance (on a phone) - is that the case? – Jason Aug 28 '19 at 20:43
  • Jason, again thank you for all your help but I've gone back and edited my post above to show you more of my code in the hope you can spot something – Journeyman1234 Aug 28 '19 at 20:44
  • are you testing on a phone or a tablet? – Jason Aug 28 '19 at 20:47
  • Using Android emulator for phone – Journeyman1234 Aug 28 '19 at 20:48
  • 1
    how do you **know** that the Master disappears? How do you normally toggle it? A gesture, a toolbar button, or what? – Jason Aug 28 '19 at 20:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198608/discussion-between-journeyman1234-and-jason). – Journeyman1234 Aug 28 '19 at 20:50
  • Jason has answered the question - it wasn't obvious to me the master was still there, but the Hamburger disappears (you can still swipe). doh. – Journeyman1234 Aug 28 '19 at 21:01