[英]Xamarin MvvmCross call command handler from another viewModel
I have two view models: ListItemViewModel
and ListViewModel
.我有两个视图模型:
ListItemViewModel
和ListViewModel
。 My ListViewModel
contains many ListItemViewModel
objects, when I try to delete one item, I'm binding DeleteCommand
in ListItemViewModel
like this:我的
ListViewModel
包含许多ListItemViewModel
对象,当我尝试删除一项时,我像这样在ListItemViewModel
绑定DeleteCommand
:
public IMvxCommand DeleteCommand => new MvxCommand(DeleteCommandHandler);
The problem is that DeleteCommandHandler
is in ListViewModel
... How can I call handler from another view model?问题是
DeleteCommandHandler
在ListViewModel
......如何从另一个视图模型调用处理程序?
UPDATE 1更新 1
Got the question why do I need this.有一个问题,为什么我需要这个。 The reason of my question is that in handler I will need to make an API call which requires dependency injection, but my
ListItemViewModel
must have default empty constructor because of automapper, so all the business logic I want to move to "parent view model" which is ListViewModel
我的问题的原因是,在处理程序中,我需要进行需要依赖注入的 API 调用,但是由于自动映射器,我的
ListItemViewModel
必须具有默认的空构造函数,因此我想将所有业务逻辑移至“父视图模型”是ListViewModel
Try using messaging services to communicate b/w different viewmodels.尝试使用消息服务以黑白不同的视图模型进行通信。 In your case, the
DeleteCommandHandler
will trigger in ListItemViewModel
and then will pass a message to ListViewModel
sending details of item deleted.在您的情况下,
DeleteCommandHandler
将在ListItemViewModel
触发,然后将消息传递给ListViewModel
发送删除项目的详细信息。
Refer to the link on how to implement Messaging.请参阅有关如何实施消息传递的链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.