M-V-VM设计问题。从视图模型调用查看 [英] M-V-VM Design Question. Calling View from ViewModel

查看:84
本文介绍了M-V-VM设计问题。从视图模型调用查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始寻找到M-V-VM的WPF应用程序。一切都有道理,到目前为止,除了这个具体问题...

I've just started looking into M-V-VM for a WPF application. Everything makes sense so far besides this particular issue...

我有一个视图模型我会打电话给搜索。该视图模型结合项目的数据网格,并列出结果。现在,我有一个需要抚养的另一种观点认为后,该项目的详细信息。

I have a ViewModel I'll call Search. This ViewModel binds to a datagrid and lists results of items. Now, I have a command that needs to bring up another view, the item's details.

把逻辑来显示搜索另一个视图命令查看看起来不正确,这是不可测试的。

Putting the logic to show another view in the Search View doesn't seem right, it's not testable at all.

下面是我的视图模型的实现,这是不可测试...

Here is my ViewModel implementation, which is not testable...

public class SearchViewModel
{
   public void SelectItem()
   {
     // I want to call the DetailsView from here
     // this seems wrong, and is untestable
     var detailsView = new DetailsView();
     detailsView.Show();
   }
}



哪里的逻辑来显示视图从视图模型的方法去这种模式?

推荐答案

查看不应该在任何地方实例化下面的UI层。虚拟机存在的境界之下,所以这是不是把那个逻辑​​(如你已经实现)的地方。

Views should never be instantiated anywhere "below" the UI layer. VMs exist below that realm, therefore this is not the place to put that logic (as you've already realized).

有几乎总是会有一些UI级事件将表明需要创建视图。在你的榜样,它可能是一排(双)点击DataGrid的事件。这将是地方新和显示你的DetailsView控件的窗口。

There will almost always be some UI level event that will indicate the need to create the view. In your example, it might be a row (double) click event on the datagrid. That would be the place to new-up and show your DetailsView window.

这篇关于M-V-VM设计问题。从视图模型调用查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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