Page Navigation - Recommended Method

dan1229dan1229 Member ✭✭

I'm working on my first Xamarin Forms application (iOS and Android) and so far whenever I've had to change pages, I just do so from the code behind file. While my application works thus far, I feel like this is wrong as the application gets bigger and there are API calls between pages, commands, etc. Specifically because I'm having a problem where I have a button that calls a command (which calls a REST API in the ViewModel) and has an event handler to change the page in the code behind.

I know you can pass the Navigation stack to the ViewModel but is this the recommended method? Does it matter at all? I'm really curious what the "official" way to do this is and how problems like the one I'm facing should be handled.

Thanks in advance!

Best Answers

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    You have not to pass NavigationStack to your viewmodel.
    Viemodel can access NavigationStack using

    Application.Current.MainPage.Navigation (if I remember the syntax)
  • JohnHardmanJohnHardman GBUniversity mod
    edited August 2018

    @dan1229 - As @ClintStLaurent suggested, it's worth having a navigation manager class, whether your own implementation or one from a 3rd party. Whether you then call into that navigation manager from your code-behind (View) or from your View Model is largely a matter of taste.

    As your application grows, you may find it useful to implement NestedCommand or MultiCommand or ChainOfCommands (or some other such name) - basically one Command that executes N other Commands in a sequence. That way, if something has to be done in code behind, then something has to be done in the View Model, then something has to be done back in the code-behind (or some other sequence), you can bind one Command to a Button (as an example), so that when the Button is pressed, the entire sequence of Commands is executed.

    I only use this in limited places, typically where a Xamarin.Forms bug requires me to do things in the View, but where the real objective is to call my Command in the View Model and then navigate to another page. It may be used in limited places, but it's a handy tool.

  • JohnHardmanJohnHardman GBUniversity mod

    @AlessandroCaliaro said:
    You have not to pass NavigationStack to your viewmodel.
    Viemodel can access NavigationStack using

    Application.Current.MainPage.Navigation (if I remember the syntax)

    A purist approach wouldn't want the View Model to know about MainPage, which is why it's common to pass Navigation into the VM.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Thanks John, I am not so purist ;)
  • dan1229dan1229 Member ✭✭
    edited August 2018

    @ClintStLaurent I was doing that to pass my User variable from page to page, I'm coming from Android where there's a similar mechanism to pass data between activities. I was planning on using the Navigation Manager I was gonna make to store a static version of this variable though, create the Nav Manager property in my App.xaml.cs and hope for the best.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @dan1229 said:
    @ClintStLaurent I was doing that to pass my User variable from page to page,

    Stop. Pages shouldn't know jack. There's a problem there with the architecture. Pages are binded to ViewModels. ViewModels know things. Pages are just reflections of data in the ViewModel and a way for the user to interact with Data. Beyond that, Pages shouldn't know or do a darned thing. They are ignorant and shouldn't affect anything. Your code (logic) should run just find even if you had no pages

Sign In or Register to comment.