关闭视图时如何传回数据 [英] How to pass data back in when closing a view

查看:74
本文介绍了关闭视图时如何传回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewModel,它的命令可以打开另一个视图:

I have a ViewModel that has a command that opens another view:

public ICommand OpenAnotherViewCommand
{
    get 
    {
        return new MvxCommand(() => ShowViewModel<AnotherViewModel>());
    }
}

到目前为止,太好了.然后在AnotherViewModel中,我希望能够回到第一个视图模型.最初,我做了这样的事情:

So far, so good. Then in AnotherViewModel I want to be able to go back to the first view model. Originally I did something like this:

public ICommand ReturnCommand
{
    get
    {
        Dictionary<string, string> parameters = new Dictionary<string, string>();
        // Add some stuff from this model to pass to the first view model

        return new MvxCommand(() => {
            ShowViewModel<FirstViewModel>(parameters);
        }
     }
 }

我在我的第一个视图模型中添加了InitFromBundle,这也起作用.但是,后来我意识到我原来的第一个视图模型仍然存在(我注意到了这一点,因为某些事件处理程序似乎多次触发!).我的ShowViewModel创建了一个新的FirstViewModel,但是旧的从未被销毁(现在看来确实很明显).因此,视图堆栈现在应该是first.

I added an InitFromBundle to my first view model and this worked too. However, then I realized that my original first view model still existed (I noticed this because of some event handlers that seemed to be firing multiple times!). My ShowViewModel created an new FirstViewModel, but the old one was never destroyed (it seems really obvious now). So the stack of views is now first -> another -> first when it ought to be just first.

因此,在脸部掌心覆盖之后,我用Close(this)替换了ReturnCommand中的ShowViewModel,现在我已经解决了导航问题,并且不会产生很多不必要的视图模型.但是,我已经失去了将数据从AnotherViewModel传递回第一个数据的能力.

So after face palming over that I replaced my ShowViewModel in ReturnCommand with Close(this) and now I've fixed the navigation problem and I'm not producing a long line of unneeded view models. However, what I've lost is the ability to pass back data from AnotherViewModel to the first one.

那么当第二个视图模型关闭时,如何将数据传递回我的第一个视图模型?

So how can I pass data back to my first view model when the second one is closed?

推荐答案

1

您可能已经知道,可以始终在视图模型中使用一种变量形式,该变量可以从另一个视图模型访问.例如使用静态变量.但是,特别是在要在整个应用程序中重复此模式时,imo并不是一个好习惯.

1

As you may already know you can always use a form of variable in your view models which is accessible from another view model. For instance using a static variable. However, imo it's not a good practice specially when you are going to repeat this pattern across your application.

就您而言,我认为您可以从 MvvmCross Messenger插件.在 MvvmCross N + 1 中查看N = 9,以获取有关实现的更多信息.还可以在此处获得示例源代码.

In your case I think you can benefit from MvvmCross Messenger plugin. Look at N=9 at MvvmCross N+1 for more information on the implementation. A sample source code is also available here.

通过使用Messenger插件,这很容易.您只需在关闭子视图之前发布一条消息即可.父视图已经订阅了接收此类消息的权限,其余的应该很简单.

By using the messenger plugin then it's easy. You simply publish a message before closing down the child view. The parent view has already subscribed to receive that kind of message and the rest should be straight forward.

这篇关于关闭视图时如何传回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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