0

I have question. I use to use Send to pass the viewmodel to show other page as shown below:

public class UsersViewModel : IUsersViewModel 
{
  void ShowCars()
  {
      MessagingCenter.Send<IUsersViewModel>(this, "ShowCarsViewPage");
  }
}

As you see above, i use this. Now i have situation that i need to pass diffrent viewmoodel inside UsersViewModel. I want to add ShowBuildings inside UsersViewModel. The problem is as it's diffrent viewmodel to be passed i cannot use this which leds me to use new keyword and pass all dependencies. How can i overcome that?

  void ShowBuildings()
  {
      MessagingCenter.Send<IBuildingsViewModel>(new Buildings(new DataStorage()), "ShowBuildingsViewPage");
  }

My first thought is to pass that view model i need to use into UsersViewModel's ctor but not sure if this is right way like to insert another view model into other view model?:

public class UsersViewModel : IUsersViewModel 
{
     private readonly IBuildingsViewModel _buildingviewmodel;
     
     UsersViewMode(IBuildingsViewModel buildingviewmodel)
     {
          _buildingviewmodel = buildingviewmodel;     
     }

     //So then:
     void ShowBuildings()
      {
          MessagingCenter.Send<IBuildingsViewModel>(_buildingviewmodel, "ShowBuildingsViewPage");
      }
}
Arie
  • 3,041
  • 7
  • 32
  • 63
  • you first need to use singleton , check this , then you can use any instance of any viewmodel anytime , you dont even need to send it. https://stackoverflow.com/questions/64285766/view-to-different-view-model-binding-in-xamarin-forms/64286130#64286130 – Shubham Tyagi Nov 17 '20 at 12:53

1 Answers1

0

Check xamarin publisher documentation. The third parameter is the payload data that is being sent.

MessagingCenter.Send<MainPage, string>(this, "Hi", "John");

Tip: Try to bundle multiple ViewModels with an interface and pass that. In this way you will not be tied to specific ViewModels interfaces.

interface IBuildingCollection : IEnumerable<Building>
{
}

class ViewModel1 : IBuildingsViewModel, IBuildingCollection
{
}

class ViewModel2 : IBuildingsViewModel, IBuildingCollection
{
}

class UsersViewModel : IUsersViewModel
{
    void ShowBuildings(IBuildingCollection collection)
    {
      MessagingCenter.Send<IUsersViewModel, IBuildingCollection>(this, "ShowBuildingsViewPage", collection);
    }
}

class ReceiverViewModel : IReceiverViewModel 
{
   public ReceiverViewModel() 
   {
       MessagingCenter.Subscribe<IUsersViewModel, IBuildingCollection>(this, "ShowBuildingsViewPage", myDelegate);
   }
   
   public void myDelegate(IBuildingCollection buildings) 
   {
       // Do something with buildings
   }
}
Botea Bogdan
  • 192
  • 3
  • 16
  • What about my solution at the bottom of my post? Can i just pass (of course using interface and not real object) other vide model into another view model? – Arie Nov 17 '20 at 09:35
  • That is not a valid solution. You are switching the sender = 'this' with another object, which is bad because you broken the pub-sub pattern. – Botea Bogdan Nov 17 '20 at 09:46