locked
Xamarin Forms - Navigation, DI and MVVM RRS feed

  • Question

  • User102055 posted

    Hi all. I've seen this post by @JamesMontemagno.

    Nice one - so you can use the Microsoft Extensions in a consistent fashion across .Net Core apps and Xamarin, including a DI container and other goodies; and it's likely that more and more goodies will be added in future.

    So I'd like to use that approach, and it's one less reason to take a dependency on a 3rd party MVVM framework.

    However, looking at James' sample code, he's doing Navigation from within Views and not ViewModels, which people say breaks the purist MVVM approach.

    Is that really so bad? If it's that bad, why hasn't XF been updated itself to include VM to VM Nav?

    Thanks Howard

    Thursday, June 25, 2020 10:49 AM

Answers

  • User89714 posted

    @HowardBayliss said: However, looking at James' sample code, he's doing Navigation from within Views and not ViewModels, which people say breaks the purist MVVM approach.

    Is that really so bad? If it's that bad, why hasn't XF been updated itself to include VM to VM Nav?

    There are different opinions about where navigation should be done. Some do it in the View, some do it in the ViewModel, no doubt some have an intermediate layer (such as some people, including me, do with ValueConverters). You might want to read https://docs.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/ if you haven't already.

    For those who want to do navigation in ViewModels, it doesn't actually require any update to XF. All it requires is for Navigation to be passed to ViewModels, using whichever injection method you prefer.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, June 25, 2020 11:00 AM

All replies

  • User89714 posted

    @HowardBayliss said: However, looking at James' sample code, he's doing Navigation from within Views and not ViewModels, which people say breaks the purist MVVM approach.

    Is that really so bad? If it's that bad, why hasn't XF been updated itself to include VM to VM Nav?

    There are different opinions about where navigation should be done. Some do it in the View, some do it in the ViewModel, no doubt some have an intermediate layer (such as some people, including me, do with ValueConverters). You might want to read https://docs.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/ if you haven't already.

    For those who want to do navigation in ViewModels, it doesn't actually require any update to XF. All it requires is for Navigation to be passed to ViewModels, using whichever injection method you prefer.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, June 25, 2020 11:00 AM
  • User102055 posted

    Hi @JohnHardman - thanks for your reply and the link. I see I could lift the Navigation service for VM to VM navigation (rhetorically I just wonder why XF doesn't have it built in already). Cheers.

    Thursday, June 25, 2020 11:29 AM
  • User53115 posted

    In my personal opinion, navigation should only be a concern of the view layer. Why should the view model be concerned about how the next view model gets displayed?

    Let's say I have an app that support phones and tablets with different layouts. On the phone, I have a list page, and when you select an item on the list, it navigates to a different page to show more details. On the tablet, I use a MasterDetailPage as a split pane to display both the list and the detail pages at the same time. When I select an item from the list, I'm not navigating. Why should my view models know or care about how the data is being presented?

    Thursday, June 25, 2020 7:54 PM
  • User89714 posted

    @JoeManke said: In my personal opinion, navigation should only be a concern of the view layer. Why should the view model be concerned about how the next view model gets displayed?

    Let's say I have an app that support phones and tablets with different layouts. On the phone, I have a list page, and when you select an item on the list, it navigates to a different page to show more details. On the tablet, I use a MasterDetailPage as a split pane to display both the list and the detail pages at the same time. When I select an item from the list, I'm not navigating. Why should my view models know or care about how the data is being presented?

    I agree. But lots of people do put navigation into the ViewModel, either by passing in Navigation (which I hate - the VM should not know about Xamarin.Forms at all IMHO - this is why I don't put ValueConverters in the VM), or by passing in an interface to a navigation service. If navigation is going to be managed by the VM, the interface to a navigation service at least keeps knowledge of XF out of the VM.

    Friday, June 26, 2020 10:40 AM
  • User53115 posted

    My current project has the views and view models very tightly coupled, because they are in the same csproj. They made a navigation service interface to use in the view models, but it includes methods that return Xamarin.Forms classes. I've been crusading to fix it since the first day I had access to the code.

    Friday, June 26, 2020 2:57 PM
  • User89714 posted

    @JoeManke said: They made a navigation service interface to use in the view models, but it includes methods that return Xamarin.Forms classes.

    I hope unit testing a VM doesn't require calling Xamarin.Forms.Forms.Init() ... ;-)

    Friday, June 26, 2020 3:07 PM
  • User53115 posted

    Several do... :( Or at least MockForms.Init()

    Friday, June 26, 2020 5:29 PM
  • User89714 posted

    @JoeManke said: Several do... :( Or at least MockForms.Init()

    Suiting up and harnessing my trusty steed, ready to join the crusade ;-)

    Friday, June 26, 2020 5:48 PM