Tuesday, December 23, 2014

MasterDetailPage example in xamarin forms

Q: How to use Master Detail Page in Xamarin Forms.
Ans:

 1. Create a Mobile Project using "Visual Studio" or "Xamarin Studio".
 
 
 2. Create a page in shared section and type the code below.
               public class MainPage : MasterDetailPage
    {
        public MainPage()
        {
            var menuPage = new MenuPage();
            menuPage.Menu.ItemSelected += (sender, e) => NavigateToPage(e.SelectedItem as                             MenuItem);
            Master = menuPage;
            Detail = new NavigationPage(new HomePage());
        }
        void NavigateToPage(MenuItem menu)
        {
            Page displayPage = (Page)Activator.CreateInstance(menu.TargetType);

            Detail = new NavigationPage(displayPage);

            IsPresented = false;
        }
    }

 3. Create a MenuPage like below
                public class MenuPage : ContentPage
               {
                         public ListView Menu { get; set; }

                         public MenuPage()
                         {
                                        Icon = "settings.png";
                                        Title = "Menu"; // The Title property must be set.
                                        BackgroundColor = Color.FromHex("333333");
                                        Menu = new MenuListView();
                                        var layout = new StackLayout
                                       {
                                               Spacing = 0,
                                               VerticalOptions = LayoutOptions.FillAndExpand
                                        };
                                        layout.Children.Add(Menu);
                                        Content = layout;
                            }
                 }

 4. Create the "ManuItem" class, like below

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

 5. Create a class inheritance listview, like below

    public class MenuListView : ListView
    {
        public MenuListView()
        {
            List<MenuItem> data = new MenuListData();

            ItemsSource = data;
            VerticalOptions = LayoutOptions.FillAndExpand;
            BackgroundColor = Color.Transparent;

            var cell = new DataTemplate(typeof(ImageCell));
            cell.SetBinding(TextCell.TextProperty, "Title");
            cell.SetBinding(ImageCell.ImageSourceProperty, "IconSource");

            ItemTemplate = cell;
            //SelectedItem = data[0];
        }
    }

public class MenuListData : List<MenuItem>
    {
        public MenuListData()
        {
            this.Add(new MenuItem()
            {
                Title = "Home",
                TargetType = typeof(HomePage)
            });

            this.Add(new MenuItem()
            {
                Title = "About Us",
                TargetType = typeof(AboutUsPage)
            });
        }
    }


 6. Create the content pages according to your requirement.
   public class HomePage : ContentPage
    {
        public HomePage()
        {
            BackgroundColor = Color.Yellow;
            Title = "Home Page";
            Content = new Label
            {
                Text = "Home Page!",
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.CenterAndExpand,
            };
        }
    }

    public class AboutUsPage : ContentPage
    {
        public AboutUsPage()
        {
            BackgroundColor = Color.Green;
            Title = "AboutUs Page";
            Content = new Label
            {
                Text = "About Us Page!",
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.CenterAndExpand,
            };
        }
    }

 7. Output



24 comments:

  1. This is probably the best, most concise step-by guide on How to use Master Detail Page in Xamarin Forms.

    Hire Xamarin Developer now to develop your Xamarin Apps.

    ReplyDelete
  2. perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | java training in velachery

    ReplyDelete
  3. I am really happy with your blog because your article is very unique and powerful for new reader.
    selenium training in chennai

    ReplyDelete
  4. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    rpa Training in Chennai

    rpa Training in bangalore

    rpa Training in pune

    blueprism Training in Chennai

    blueprism Training in bangalore

    blueprism Training in pune

    rpa online training

    ReplyDelete
  5. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command

    rpa Training in tambaram

    blueprism Training in tambaram

    automation anywhere training in tambaram

    iot Training in tambaram

    rpa training in sholinganallur

    blue prism training in sholinganallur

    automation anywhere training in sholinganallur

    iot training in sholinganallur

    ReplyDelete
  6. Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 

    Data Science Training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune
    Data science training in kalyan nagar
    selenium training in chennai

    ReplyDelete
  7. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
    java training in chennai | java training in bangalore

    java online training | java training in pune

    java training in chennai | java training in bangalore

    ReplyDelete
  8. I appreciate that you produced this wonderful article to help us get more knowledge about this topic. I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!
    python training in pune
    python online training
    python training in OMR

    ReplyDelete
  9. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Devops training in velachery
    Devops training in annanagar
    Devops training in sholinganallur

    ReplyDelete
  10. I love the blog. Great post. It is very true, people must learn how to learn before they can learn. lol i know it sounds funny but its very true. . .
    Blueprism training institute in Chennai

    Blueprism online training

    Blue Prism Training Course in Pune

    ReplyDelete
  11. Nice tips. Very innovative... Your post shows all your effort and great experience towards your work Your Information is Great if mastered very well.


    angularjs Training in chennai

    angularjs-Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    ReplyDelete
  12. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  13. Thanks for your great and helpful presentation I like your good service.I always appreciate your post.That is very interesting I love reading and I am always searching for informative information like this.Well written article Thank You for Sharing with Us pmp training Chennai | pmp training centers in Chennai | pmp training institutes in Chennai | pmp training and certification in Chennai | pmp training in velachery

    ReplyDelete
  14. Anyhow I am here now and would just like to say thanks a lot for a tremendous post and an all-round exciting blog
    safety course in chennai

    ReplyDelete
  15. I am really very happy to find this particular site. I just wanted to say thank you for this huge read!! I absolutely enjoying every petite bit of it and I have you bookmarked to test out new substance you post.

    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  16. I would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end. Thanks
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  17. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...

    Article submission sites
    Guest posting sites

    ReplyDelete