mvvmcross IOS:如何从ViewModel回调到View [英] mvvmcross IOS: How to callback from a ViewModel to a View
问题描述
我有一个MvxViewController,在ViewDidLoad中,我将按钮绑定到视图模型.单击按钮后,我将打开另一个视图,在该视图中,我需要将字符串返回到我的第一个视图
I have a MvxViewController and in the ViewDidLoad i bind the button click to the viewmodel. When the button is clicked I open another view in which I will need to return a string back to my first view
public override void ViewDidLoad ()
{
var set = this.CreateBindingSet<MyView1, MyView1ViewModel>();
set.Bind(myButton).To(vm => vm.MyButtonCommand);
set.Apply();
}
public ICommand MyButtonCommand
{
get
{
_myButtonCommand = _myButtonCommand ?? new MvxCommand(MyButtonCommandClick);
return _myButtonCommand;
}
}
private void MyButtonCommandClick()
{
ShowViewModel<ViewModelNumber2>();
}
在第二个视图中运行了一些逻辑之后,我想返回字符串
After some logic is ran in my second view I want to return the string
private void SomeMethodInViewModelNumber2()
{
//Raise event that will get pickup up in MyView
//Or somehow get "SomeString"
if (OnMyResult != null)
OnMyResult ("SomeString");
}
问题是我不想使用Messenger发送回字符串.我有我的理由,但基本上是因为ViewModelNumber2可以从许多不同的地方打开,并且工作原理略有不同,并且管理需要发送回的不同消息以及在何处订阅这些消息会很麻烦
The problem is that I don't want to send the string back using the messenger. I have my reasons but basically because ViewModelNumber2 can be opened from many different places and works slightly different and managing the different messages that would need to be sent back and where to subscribe to these messages would be a mess
有什么办法可以做下面的事情吗?
Is there any way that I can do something like the below?
public override void ViewDidLoad ()
{
var set = this.CreateBindingSet<MyView1, MyView1ViewModel>();
set.Bind(myButton).To(vm => vm.MyButtonCommand).OnMyResult((myString) => {Process(myString)});
set.Apply();
}
或者也许当我创建ViewModelNumber2时,我应该将callBack传递给构造函数,并使用该回调将字符串从ViewModelNumber2发送回MyView1ViewModel
Or perhaps when I create ViewModelNumber2 I should pass a callBack into the constructor and use that to send the string back from ViewModelNumber2 to MyView1ViewModel
ShowViewModel<ViewModelNumber2>(OnMyResult);
做到这一点的最佳方法是什么?
What is the best way to do this?
推荐答案
总之:我不知道什么是最好的方法".
In short: I don't know what "the best way to do this" is.
ChildViewModel-ParentViewModel消息的区域很复杂-尤其是因为在使用活动的Android和使用Pages的WindowsPhone的平台上,您无法保证在显示Child时ParentViewModel将在内存中. (注意:这在iOS上不是问题,因为它的应用暂停"模型更简单)
The area of ChildViewModel-ParentViewModel messages is complicated - especially because on platforms like Android using Activities and WindowsPhone using Pages you have no guarantee that the ParentViewModel will be in memory when the Child is shown. (Note: this isn't a problem on iOS as its "app suspension" model is simpler)
当我确实需要一个ViewModel将数据返回到另一个ViewModel时,
When I do need one ViewModel returning data to another, then:
-
通常,我会尝试将数据收集视图实现为弹出对话框"而不是整个页面",这使父子ViewModel关系更加正确-并确保当孩子关闭.
Often I try to implement the data collection views as "popup dialogs" rather than as "whole pages" - this makes the parent-child ViewModel relationship more correct - and ensures the parent ViewModel will be in memory when the child closes.
通常,我建议人们使用像Greg所述的基于Messenger的技术,该技术在
Often I recommend people use a Messenger-based technique like Greg describes in: http://www.gregshackles.com/2012/11/returning-results-from-view-models-in-mvvmcross/
- 通常我是通过后台服务而不是通过ViewModel-ViewModel消息传递来完成此消息传递的(有点像在我使用的另一个解决方案是:
Another solution I've used is to:
- 使用
void Deposit(key, value)
和bool TryCollect(key, out value)
之类的API实现 - 允许关闭的子级" ViewModel在关闭时保留值" 我的父级" ViewModel中的
- 实现
IVisible
功能-如
IDropBoxService
单例- implement a
IDropBoxService
singleton - with an API likevoid Deposit(key, value)
andbool TryCollect(key, out value)
- allow the closing "child" ViewModels to leave "values" when they close
- implement
IVisible
functionality in my "parent" ViewModel - like in https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-42-Lifecycles/Lifecycle.Core/ViewModels/FirstViewModel.cs#L10 - use the
IVisible
method to check for messages
要完美实现任何事情,您确实应该添加序列化代码,以确保这些代码在所有平台上的逻辑删除"过程中均能正常工作……但这通常是过大的-对于简单的数据收集对话框,用户通常不需要完美" 墓碑支持.
To implement anything perfectly, you really should add serialisation code to make sure this all works during "tombstoning" on all platforms... but often this is overkill - for a simple data collection dialog users often don't need "perfect" tombstoning support.
这篇关于mvvmcross IOS:如何从ViewModel回调到View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 使用