0
votes

I have an mvvmcross xamarin project I am trying to figure out the best way I should approach sending data across screens for a particular situation.

My first screen is a search filter, it contains all kinds of criteria for the users to select for filtering results. The second screen is the results list screen.

I've seen numerous options for approaching this problem:

1) Use the Init method - The issue I'm having here is that when I pass custom objects, the values are always null/default. If I pass a string it works fine. If I could pass custom objects, I would either pass the EntityFilter or a List of Entities which is the results. I do have to query the entities on the filter page because what they select on one filter can affect others based on what entities remain. I have added the following to the AssemblyInfo.cs and it still doesn't work.

[assembly: InternalsVisibleTo("Cirrious.MvvmCross")]

2) Use Messaging. The problem here is that the result page isn't created when the filter is changed, so messaging won't work.

3) Use an in memory object. I created a results service that contains nothing but a public property of List of Entities. It's registered as lazy singleton in the IoC (as are all services). This method works and is my current solution. But it doesn't "feel" right. It feels like I'm doing something wrong or breaking best practices. Is this the way it should be done?

1

1 Answers

0
votes

This is a fairly frequently asked question.

There's a recent question about using custom types for navigation at: Custom types in Navigation parameters in v3 - note that the original poster proposes one solution in their question there.

There are also several other questions around on this topic. The basic answer for all of these is that on many platforms navigation uses serialization techniques - e.g. Xaml Urls or Android Intents. Because of this, and because 'tombstoning' means that you can never be sure a "new" page will be in the same memory space as the "old" one, then by default MvvmCross insists on using simple serializable types for all ShowViewModel navigation.

This same reasoning for why navigation only allows simple serializable types is repeated all the way back to MvvmCross v1 - e.g. What is the best way to pass objects to "navigated to" viewmodel in MVVMCross?

The good news is that this topic is also getting some love in the official project docs - but these docs are still a bit of a work in progress - e.g. see https://github.com/MvvmCross/dev.mvvmcross.com/blob/master/_posts/1970-01-01-page-navigation.markdown