Is there a MVVM way to bind MasterDetailPage Detail?

WinterCloudWinterCloud GBMember ✭✭✭
edited April 2017 in Xamarin.Forms

I got multiple details pages, they are different and have different page models.

All the example I can find is using event, itemselected of the Master Page Menu, and just create a page based on the page type registered with the menu item. Like here:
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/navigation/master-detail-page/

Is there a MVVM way, so I can bind Detail, and display correct detail page by binding the detail page model.

I'm not using any MVVM package, just native Xamarin.Forms code.

I'm not sure what the detail page model type is going to be, a page model base or object I guess.

Please advise. Thanks in advance.

For example:

`

    class MasterPageModel 
    {
        private object _detailPage;

        public MasterHomePageModel MasterMenuPage { get; set; }

        public object DetailPage
        {
            get { return _detailPage; }
            set { SetProperty( ref _detailPage, value ); }
        }
    }

`

Best Answer

Answers

  • WinterCloudWinterCloud GBMember ✭✭✭

    @mikeacosta said:
    Not sure if this is what you're after, but I just populate the menu ListView with view model instances, with a string property for each page class


    menuList.ItemsSource = new List<MenuItemViewModel>() { new MenuItemViewModel() {Sequence = 1, Text = "About", Page = "AboutPage" }, new MenuItemViewModel() {Sequence = 2, Text = "This is a page", Page = "ThisPage" }, new MenuItemViewModel() {Sequence = 3, Text = "Another page", Page = "AnotherPage" } };

    and then do the item selected like this


    MenuItemViewModel vm = e.SelectedItem as MenuItemViewModel; Page selectedPage = (Page)Activator.CreateInstance(Type.GetType("MyApplicationName.Views." + vm.Page + ",MyApplicationName")); RootPage rootPage = (Application.Current.MainPage as RootPage); rootPage.Detail = new NavigationPage(selectedPage);

    HTH

    Thanks for the reply, I believe this will work.

    I've seen this approach somewhere, as my project can't use magic strings and I'm trying to stay away from reflection, I didn't use this approach.

  • mikeacostamikeacosta USUniversity ✭✭✭

    Yeah, probably an enum for pages would be a little better.

Sign In or Register to comment.