2
votes

in a main viewmodel where i collect data from another viewmodels, I created in summary two or three public Init methods with different signatures. When i navigate back to the base viewmodel from the other viewmodels with ShowViewModel, I awaited that the right Init method will be executed, but this don't happen. Regarding the greet practical documentation here:

http://slodge.blogspot.ch/2013/03/v3-new-viewmodel-lifecycle.html

This should be work :-/.

I will explain this with some code.

My main view model is e.g.:

public class MainViewModel : MvxViewModel
{
     MainViewModel() {}

     public class ParameterFirst
     {
         public string Id { get; set; }
     }

     public class ParameterSecond
     {
         public string Id { get; set; }
     }

     public class ParameterSecond
     {
         public string Id { get; set; }
     }

     public class ParameterThird
     {
         public string Id { get; set; }
     }

     public void Init(ParameterFirst objFirst)
     {
          //do something
     }

     public void Init(ParameterSecond objSecond)
     {
          //do something
     }

     public void Init(ParameterThird objThird)
     {
          //do something
     }
}

Then I will navigate from another viewmodel and await that the right Init method will be executed:

public class CollectData_ONE_ViewModel : MvxViewModel
{
     CollectData_ONE_ViewModel() {}

     public void DidWork()
     {
          //Hopefully the Init method with argument ParameterFirst should be called
          base.ShowViewModel<MainViewModel>(new MainViewModel.ParameterFirst { Id = "11" });
     }
}

next here the second viewmodel

public class CollectData_SECOND_ViewModel : MvxViewModel
{
     CollectData_SECOND_ViewModel() {}

     public void DidWork()
     {
          //Hopefully the Init method with argument ParameterFirst should be called
          base.ShowViewModel<MainViewModel>(new MainViewModel.ParameterSecond { Id = "22" });
     }
}

and the third viewmodel

public class CollectData_THIRD_ViewModel : MvxViewModel
{
     CollectData_THIRD_ViewModel() {}

     public void DidWork()
     {
          //Hopefully the Init method with argument ParameterFirst should be called
          base.ShowViewModel<MainViewModel>(new MainViewModel.ParameterThird { Id = "33" });
     }
}

In my code, each time the First Init method is called, I'm really at the end and don't have further ideas :) Did anyone here experienced the same issue? Or do anyone here have another Idea to collect data to the main viewmodel in an elegant way? Thanks a lot in advance for reading :)

1
Okay, now I made a new experience, all three Init methods will be called in sequence and the Init method with the suitable signature get the passed object. So an argument check for null is absolutely needed in any Init method. Nevertheless, many thanks to them they read and thought for a solution. - Mehmet
Please do answer your own question and mark it as solved. - Cheesebaron
Question is answered but there is another problem due the fact that all Init methods are called and the objects in the signature could have same properties, there is a difficulty tu separate the entry point. - Mehmet

1 Answers

2
votes

The Init mechanism in MvvmCross is deliberately lightweight. If you declare multiple methods, all of them will be called - this is by design. Also if some of the Init parameter objects were to share properties then these would clash - see Custom types in Navigation parameters in v3

As it says in the blog post you reference "generally you will probably only want to use one within your application" - so I'd recommend refactoring to a single navigation parameter object and using your own ViewModel-based logic to decide how your ViewModel should initialise.

If you really do need three Init methods called in three different situations, then you can easily pack and unpack your own parameter objects using a custom method (possibly in a BaseViewModel class) like in https://stackoverflow.com/a/19059938/373321