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



47 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. I am really happy with your blog because your article is very unique and powerful for new reader.
    selenium training in chennai

    ReplyDelete
  3. 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
  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.
    java training in chennai | java training in bangalore

    java online training | java training in pune

    java training in chennai | java training in bangalore

    ReplyDelete
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. Great and Nice Blog! I would like to thank for the efforts you have made in writing this great article. Thanks for sharing.


    Data Science Courses

    ReplyDelete
  12. Amazing knowledge and I like to share this kind of information with my friends and hope they like it they why I do
    Data Science Course in Pune

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. A debt of gratitude is in order for sharing the information, keep doing awesome... I truly delighted in investigating your site. great asset...
    rpa training in malaysia

    ReplyDelete
  15. An outstanding share! I have just forwarded this onto a colleague who had been doing a little research on this. And he actually bought me dinner because I found it for him... lol. So let me reword this.... Thank YOU for the meal!! But yeah, thanks for spending time to talk about this matter here on your blog.

    ReplyDelete
  16. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.

    PMP Certification

    ReplyDelete
  17. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.


    Data Science Course in Pune

    ReplyDelete
  18. Excellent Blog! I would like to thank for the efforts you have made in writing this post.


    Business Analytics Course

    ReplyDelete
  19. Great post i must say and thanks for the information.

    Data Analytics Courses

    ReplyDelete
  20. Great post i must say and thanks for the information.

    Data Scientist Course

    ReplyDelete
  21. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog of salesforce developer training . Really very informative post you shared here. Kindly keep blogging...
    sap training in chennai

    sap training in tambaram

    azure training in chennai

    azure training in tambaram

    cyber security course in chennai

    cyber security course in tambaram

    ethical hacking course in chennai

    ethical hacking course in tambaram

    ReplyDelete
  22. I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.
    a href="https://www.excelr.com/data-analytics-certification-training-course-in-pune/"> Data Analytics Course in Pune/">Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also.
    I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…

    ReplyDelete
  23. Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging. After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    Tableau Online Training

    ReplyDelete
  24. I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.
    Data Analytics Course in PuneWow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also.
    I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…

    ReplyDelete
  25. Attend The Business Analytics Courses From ExcelR. Practical Business Analytics Courses Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Business Analytics Courses.
    Business Analytics Courses

    ReplyDelete
  26. Cool stuff you have and you keep overhaul every one of us
    pmp certification in bangalore

    ReplyDelete
  27. Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Visit here for Penetration testing services and Software testing services

    ReplyDelete
  28. I have read your article, it is very informative and helpful for me.I admire the valuable information you offer in your articles. Thanks for posting it..
    data science online training in hyderabad

    ReplyDelete
  29. Very educating story, saved your site for hopes to read more! data science course in kanpur

    ReplyDelete
  30. Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also.
    full stack web development course in malaysia


    ReplyDelete
  31. A debt of gratitude is in order for sharing the information, keep doing awesome... I truly delighted in investigating your site. great asset...
    online grocery delivery

    ReplyDelete