0

I am having 2 views in my application and each of them having associated view model .I am having a requirement like access the second viewmodel methods form the first view's xaml.cs file (I don't want to bring all the logic in second viewmodel to first one ,that's why keeping both view model as such )

Is there any way i can achieve the same ?

Ansi P.K
  • 31
  • 4
  • Does this answer your question? [MVVM Best Practices: communication between view models](https://stackoverflow.com/questions/46984421/mvvm-best-practices-communication-between-view-models) – Cfun Oct 09 '20 at 18:58
  • You will get a better help if you provide more details about the relation between your VM. Maybe make them static methods? – Cfun Oct 09 '20 at 19:00

1 Answers1

0

You can do that by making a ViewModelLocator using MVVMLight, it will create a singleton pattern and you can access ViewModel anywhere using ServiceLocator.

https://www.c-sharpcorner.com/article/xamarin-forms-mvvm-viewmodel-locator-using-mvvm-light/

Following the link and then you can write

App.ViewModelLocator.YourViewModel.YourMethod()

or better

App.ViewModelLocator.YourViewModel.YourCommand.Execute()

Shubham Tyagi
  • 798
  • 6
  • 21