【问题标题】:View to different view model binding in Xamarin forms在 Xamarin 表单中查看到不同的视图模型绑定
【发布时间】:2020-10-09 18:52:06
【问题描述】:

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

有什么方法可以达到同样的效果吗?

【问题讨论】:

标签: xamarin xamarin.forms viewmodel


【解决方案1】:

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

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

点击链接,然后你就可以写了

App.ViewModelLocator.YourViewModel.YourMethod()

或更好

App.ViewModelLocator.YourViewModel.YourCommand.Execute()

【讨论】:

    猜你喜欢
    • 2018-04-09
    • 2020-10-28
    • 2017-06-03
    • 2022-10-18
    • 2018-04-19
    • 2016-12-17
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    相关资源
    最近更新 更多