MasterDetailPage not loading Detail pages

Working on my firs MDP and can't figure out why it's not loading any of the Detail pages while it's loading the Master page just fine. I'm sure there's just some naming convention issue, but I can't find it for the life of me. Would anyone mind takign a sec to parse my code?

MainPage.Xaml
<?xml version="1.0" encoding="utf-8" ?>

    <MasterDetailPage.Master>
        <ContentPage Title="Menu" 
                     BackgroundColor="#e8e8e8" 
                     >
            <StackLayout  Orientation="Vertical">
                <StackLayout BackgroundColor="#3498db"
                             HeightRequest="75">
                    <Label Text="Navigation Menu"
                           FontSize="20"
                           VerticalOptions="CenterAndExpand"
                           TextColor="White"
                           HorizontalOptions="Center"/>
                </StackLayout>

                <ListView 
                    x:Name="navigationDrawerList"
                    RowHeight="60"
                    SeparatorVisibility="None"
                    BackgroundColor="#e8e8e8"
                    ItemSelected="OnMenuItemSelected">

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout
                                    VerticalOptions="FillAndExpand"
                                    Orientation="Horizontal"
                                    Padding="20,10,0,10"
                                    Spacing="20">

                                    <Image Source="{Binding Icon}"
                                           WidthRequest="40"
                                           HeightRequest="40"
                                           VerticalOptions="Center"/>

                                    <Label Text="{Binding Title}"
                                           FontSize="Medium"
                                           VerticalOptions="Center"
                                           TextColor="Black"/>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </StackLayout>
        </ContentPage>
    </MasterDetailPage.Master>

    <MasterDetailPage.Detail>
        <NavigationPage>

        </NavigationPage>
    </MasterDetailPage.Detail>
</MasterDetailPage>

MasterPage.Xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Attempt2.NavMenuItems;
using Attempt2.Views;


namespace Attempt2
{
    public partial class MainPage : MasterDetailPage
    {
        //Create the public list of you navigation menu.
        public List<MasterPageItem> menuList { get; set; }

        public MainPage()
        {
            InitializeComponent();
            //declare the variable used above as a list of master page items.
            menuList = new List<MasterPageItem>();

            //Create the pages for menu navigation.
            //Define the title, icon, and target type.
            var page1 = new MasterPageItem() { Title = "Item 1", Icon = "itemIcon1.png", TargetType = typeof(Page1) };
            var page2 = new MasterPageItem() { Title = "Item 2", Icon = "itemIcon2.png", TargetType = typeof(Page2) };

            //Adding menu items to menuList
            menuList.Add(page1);
            menuList.Add(page2);

            //Setting our list to be ItemSource for ListView in MainPage.xaml
            navigationDrawerList.ItemsSource = menuList;

            //Initial Navigation, set to home page.
            Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(Page1)));

        }

        //Event for menu item selection. HHandle navigation based on user selection in menu ListView
        private void OnMenuItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var item = (MasterPageItem)e.SelectedItem;
            Type page = item.TargetType;

            Detail = new NavigationPage((Page)Activator.CreateInstance(page));
            IsPresented = false;
        }
    }
}

Page1.xaml

    <Label Text="This is Test Page 1." TextColor="White" VerticalOptions="Center" HorizontalOptions="Center"/>
</ContentPage>

Answers

  • Richy_GeorgeRichy_George INMember ✭✭✭

    set a navigation in app.cs page
    MainPage = new MainPage();

    and MainPage XAML
    <?xml version="1.0" encoding="utf-8" ?>

    <MasterDetailPage.Master>

    </MasterDetailPage.Master>
    <MasterDetailPage.Detail>


    //set your corresponding page


    </MasterDetailPage.Detail>

    and in MainPage.xaml.cs
    using System;
    using Xamarin.Forms;

    namespace App2 //set your name space
    {
    public partial class MainPage : MasterDetailPage
    {

        public MainPage()
        {
            InitializeComponent();
            masterPage.ListView.ItemSelected += OnItemSelected;
        }
        void OnItemSelected(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;
            }
        }
    }
    

    }

    and MasterPage.xaml
    <?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="App2.MasterPage"

             Icon="hamburger.png"                      // set menu icon
             Title="Personal Organiser"
             BackgroundColor="#ffffff">
    

    <ContentPage.Content >


    <ListView.ItemTemplate>





    and MasterPage.xaml.cs
    using System.Collections.Generic;
    using Xamarin.Forms;

    namespace App2 //set your name space
    {
    public partial class MasterPage : ContentPage
    {
    public ListView ListView { get { return listView; } }
    public MasterPage()
    {
    InitializeComponent();
    var masterPageItems = new List();

            masterPageItems.Add(new MasterPageItem
            {
                Title = "Home",                                     // set your title
                IconSource = "home.png",                   //set your image
                TargetType = typeof(LivePage)            //set corresponding page
            });
    
            masterPageItems.Add(new MasterPageItem
            {
                Title = "Profile",
                IconSource = "account.png",
                TargetType = typeof(ProfilePage)
            });
            masterPageItems.Add(new MasterPageItem
            {
                Title = "Settings",
                IconSource = "settings.png",
                TargetType = typeof(SettingsPage)
    
            });
            masterPageItems.Add(new MasterPageItem
            {
                Title = "Terms and Conditions",
                IconSource = "terms.png",
                TargetType = typeof(TermsPage)
            });
            masterPageItems.Add(new MasterPageItem
            {
                Title = "Help",
                IconSource = "help.png",
                TargetType = typeof(HelpPage)
            });
            masterPageItems.Add(new MasterPageItem
            {
                Title = "Notifications",
                IconSource = "notifications.png",
                TargetType = typeof(NotificationPage)
            });
            masterPageItems.Add(new MasterPageItem
            {
                Title = "Logout",
                IconSource = "exit.png",
                TargetType = typeof(LogoutPage)
            });
            listView.ItemsSource = masterPageItems;
        }
    
    }
    

    }

    and set the model MasterPageItem.cs
    using System;
    namespace App2
    {
    public class MasterPageItem
    {
    public string Title { get; set; }

        public string IconSource { get; set; }
    
        public Type TargetType { get; set; }
    }
    

    }

Sign In or Register to comment.