View to different view model binding in Xamarin forms

我与影子孤独终老i 提交于 2021-02-05 12:01:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!