Search code examples
androidxamarinviewmodelmvvmcross

Passing Data Between Viewmodels MvvmCross


I am working on Xamarin Android Application and I am using MvvmCross.I am not able to pass and retrieve data between Viewmodels. What is the solution ?


Solution

  • That depends on what data you want to retrieve. For objects look at this answer from Stuart.

    And when you want to send/retrieve non-object data, you can do that with the build in viewmodel-navigation like following example:

    // Navigate to viewmodel with parameters
    var param = new Dictionary<string, string>
    {
        {"key1", "value for key 1"
        {"key2", 12}
    };
    
    ShowViewModel<MyViewModel>(param);
    

    And then retrieve the Parameters in your MyViewModel:

    protected override void InitFromBundle(IMvxBundle parameters)
    {
        if (parameters.Data.ContainsKey("key1"))
        {
            var mykey1value = parameters.Data["key1"]
        }
    
        // And so on
    
        base.InitFromBundle(parameters);
    }