以 Xamarin 形式查看到不同的视图模型绑定 [英] View to different view model binding in Xamarin forms

查看:30
本文介绍了以 Xamarin 形式查看到不同的视图模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有 2 个视图,每个视图都有关联的视图模型.我有一个要求,例如从第一个视图的 xaml.cs 文件中访问第二个视图模型方法(我不想将所有逻辑在第二个视图模型到第一个视图模型中,这就是为什么保持两个视图模型一样)

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 ?

推荐答案

您可以通过使用 MVVMLight 创建一个 ViewModelLocator 来实现这一点,它将创建一个单例模式,您可以使用 ServiceLocator 在任何地方访问 ViewModel.

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/

点击链接然后你可以写

App.ViewModelLocator.YourViewModel.YourMethod()

App.ViewModelLocator.YourViewModel.YourMethod()

或更好

App.ViewModelLocator.YourViewModel.YourCommand.Execute()

App.ViewModelLocator.YourViewModel.YourCommand.Execute()

这篇关于以 Xamarin 形式查看到不同的视图模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆