0
votes

I have taken the approach shown in this Xamarin Forms demo.

https://github.com/xamarin/xamarin-forms-samples/tree/master/Navigation/MasterDetailPage/MasterDetailPageNavigation

However when I create a new MainPage, Master and Detail are both null... I expected at it would be my MenuPage and TabPage respectively. Is this a bug or am I doing something fundamentally wrong?

MainPage:

<MasterDetailPage  xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:ctr="clr-namespace:MyApp.Core.Views;assembly=MyApp.Core"
         x:Class="SmartMiner.Core.Views.MainPage"
         >
<MasterDetailPage.Master>
    <ctr:MenuPage x:Name="MenuPage"/>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
    <ctr:TabPage x:Name="TabPage" />
</MasterDetailPage.Detail>

MenuPage(Master)

<ContentPage 
xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
Title="Menu"
x:Name="MenuPage"
x:Class="MyApp.Core.Views.MenuPage"
>
<ContentPage.Content>
</ContentPage.Content>

TabPage(Details)

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
x:Class="MyApp.Core.Views.TabPage"
xmlns:tab="clr-namespace:SmartMiner.Core.Views;assembly=SmartMiner.Core"
Title="Tabs Page"
>
<tab:Tab1 Title="Tab One"/>
<tab:Tab2 Title="Tab Two" />
<tab:Tab3 Title="Tab Three" />

Init

protected override void OnStart(){
        var mainPage = new MainPage();
        if (Device.RuntimePlatform == Device.iOS)
            MainPage = mainPage;
        else
            MainPage = new NavigationPage(mainPage); 
    }

The result when inspecting var mainPage is that the properties Master and Detail are null and the designation of MainPage throws an error

"Master and Detail must be set before assigning it to MainPage"

1
What do you mean by both null?theMayer
I have updated my question to include what the result is.Mark Hollas
MainPage = new MainPage(); put his line in App(), Not put in OnStart()Ziyad Godil
Thanks, I have tried in the App init as well as currently in OnStart... both yield the same resultMark Hollas
See my answer. The nav page is for the detail page, not the masterdetail pageSteve Chadbourne

1 Answers

1
votes

I can spot a couple of issues.

The main page needs to be created and assigned in the App constructor. Also don't wrap MainPage in a NavigationPage. You do that in your detail page.

Your App.xaml.cs should look like this:

   public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

             MainPage = new MainPage();
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }

Here is the master detail page (your MainPage)

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MasterDetailTest.MainPage"
             xmlns:ctr="clr-namespace:MasterDetailTest">
  <MasterDetailPage.Master>
    <ctr:MenuPage x:Name="MenuPage" />
  </MasterDetailPage.Master>
  <MasterDetailPage.Detail>
    <NavigationPage>
      <x:Arguments>
        <ctr:TabPage x:Name="TabPage" />
      </x:Arguments>
    </NavigationPage>
  </MasterDetailPage.Detail>
</MasterDetailPage>

Menu page

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MasterDetailTest.MenuPage"
             Title="Menu">
    <ContentPage.Content>
    </ContentPage.Content>
</ContentPage>

Tab Page

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:tab="clr-namespace:MasterDetailTest;assembly=MasterDetailTest"
             x:Class="MasterDetailTest.TabPage">
    <tab:Tab1Page Title="Tab 1" />
    <tab:Tab2Page Title="Tab 2" />
</TabbedPage>

And one of the tab pages

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MasterDetailTest.Tab1Page">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Tab One"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>