问题
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 ?
回答1:
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()
来源:https://stackoverflow.com/questions/64285766/view-to-different-view-model-binding-in-xamarin-forms