0

I'm currently working on a Xamarin.forms project using .NET Standard as code sharing strategy. This app use multiple pages/views. I try to use the MVVM pattern by using the MvvmLightLibsStd10 library. I already successfully setup the MVVM structure just like my previous problem:

Xamarin.Forms How to switch pages using MVVMLight

I following the same code structure. Every view (page) has his own ViewModel with all the logic. In the app there is a page that contains a Listview with objects. When an object is chosen, that object need to be passed to my second page viewmodel since I need to do something with this object in my secondVM. I already can get the selectedObject from the list in the Viewmodel of the listview view/page. I like to respect the MVVM concept as close as possible, so I to try to avoid code behind of a view as much as possible.

Sure, there must be a way to build a multipaged app with MVVM and pass data between the pages. But I really don't know how to do this. Since I am novice user to MVVM in Xamarin.forms.

I like to keep The header with back button navigation in tact. Thanks in advance.

appyGeek
  • 45
  • 1
  • 8
  • ...MVC and all the MV* variants have always had a tendency to shine the best when interwoven with a Message Oriented system. Data comes back from a service and a service controller broadcasts data to a topic. Other controllers subscribe to the topic and automatically have their data synchronized. Try taking a look into Reactive Extensions and Observables. – K. Alan Bates Feb 27 '18 at 21:12
  • Prism have NavigationParameters for this specific task. Maybe ditch MVVMLight and use Prism? – Julius Degutis Feb 27 '18 at 21:30

1 Answers1

0

K. Alan Bates in the comments has the right idea about using a Message Oriented system to pass data between different pages. MVVMLight includes an IMessenger interface for this purpose, and would be my recommendation for passing data between pages that should be decoupled from each-other.

In this case it sounds like you're dealing with completely different ViewModels, but if you find that your ViewModels are logically coupled (as in, it makes sense for them to be associated), it's fine for a ViewModel to contain a reference to another ViewModel, doesn't break MVVM pattern at all to my knowledge.

Thorin Jacobs
  • 290
  • 2
  • 9