Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Menu, Navigation concepts with MasterDetailPage and MVVM based approach from XLabs

$
0
0

Hi @all,

i am really new since a few weeks on CSharp and Xamarin (using Xamarin.Forms) and comes from the PHP, HTML World so its hard for me to understand some things. So please dont get to hard with me. I known that MVVM is not the simples way at beginning but i love good and clear concepts.

Currently i try to get a good/best practice learning for building a navigation with a MasterDetailPage and using the MVVM ViewFactory from XLabs. If i understand the MVVM right its a good practice to let the ViewModel make things like Navigation.Push and let the View only show the UI and bind the interaction from user to the ViewModel. So if i want to navigate from one page to a other page the way is View <> ViewModel > Navigate.Push...

But on a MasterDetailPage i had the NavigationView() on the "Detail" and the "Master" is a simple ContentPage with a ListView or for testing a simple Button how i can tell at this point the Detail to Navigate to a other ViewModel?

Is one way to create the master as ViewModel / View also? And use the NavigationService from ViewsFactory? Remember i search a nice discussion about best practice and solutions i have seen a lot of examples and every example confused me more and more...

Thanks all for time!

In my app.css :

    public static Page GetMainPage ()
    {
        ViewFactory.EnableCache = false;
        // Register ImprintPage as MVVM Page because of assembly data like verisionsnumber will be added to info
        ViewFactory.Register<ImprintPage, ImprintViewModel> ();

        var mainPage = new NavigationView (ViewFactory.CreatePage<ImprintViewModel, ImprintPage> () as Page) {
            BarBackgroundColor = Color.FromRgb (241, 169, 18),
            BarTextColor = Color.White,
        };

        Resolver.Resolve<IDependencyContainer> ().Register<INavigationService> (t => new NavigationService (mainPage.Navigation));

        return new MainViewMasterPage (mainPage);
    }

My MasterDetailPage:

public class MainViewMasterPage : MasterDetailPage
{
    public MainViewMasterPage (Page mainPage)
    {
        Master = new MainMenuMasterView ();
        Detail = mainPage;
    }           
}

My MainMenuMasterView:

public class MainMenuMasterView : BaseView
{
    public MainMenuMasterView ()
    {
        Title = "Menü";
        Icon = Device.OnPlatform (
            iOS: "toolbar_menu.png",
            Android: "toolbar_menu.png",
            WinPhone: "toolbar_menu.png"
        );

        BackgroundColor = Color.FromRgb (50,48,53);
        Padding = new Thickness (0, Device.OnPlatform (20, 0, 0), 0, 0);

        var listView = new ListView ();
        listView.ItemsSource = new [] { "a", "b", "c" };
        listView.ItemTapped += (sender, e) => {
            // await DisplayAlert("Tapped", e.Item + " row was tapped", "OK");
            Debug.WriteLine("Tapped: " + e.Item);
            ((ListView)sender).SelectedItem = null; // de-select the row
        };

        Content = new StackLayout { 
            Children = {
                listView,
            }
        };
    }
}

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>