M-V-VM Design Question. Calling View from ViewMode

2019-01-21 23:19发布

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();
   }
}

Where does the logic to show a view from a ViewModel method go in this pattern?

标签: c# wpf mvvm
5条回答
等我变得足够好
2楼-- · 2019-01-22 00:09

We use a variant on this pattern, Here we have controllers that represent the VM, so the datacontext of the View is the VM and our DTOs are properties of the VM/Controller. We call it a controller still as we use this as the control point and thus handle certain command from the View. This is (I think) where we would implement the command such as yours.

查看更多
走好不送
3楼-- · 2019-01-22 00:15

Here's a basic rule of thumb on this.

  • If you are handling local actions in your view, you can intiate from the view model.

  • If it's cross view (like showing a search screen), then either use an EventAggregator pattern (an eventing service) or inject an Application Controller which you invoke methods on, and it in turn displays the search.

查看更多
混吃等死
4楼-- · 2019-01-22 00:17

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).

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.

查看更多
老娘就宠你
5楼-- · 2019-01-22 00:18

Catel includes an approach which involves the use of IUIVisualizerService. This interface defines a UI controller which can be used to display dialogs in either modal or modaless form from a ViewModel. Basically, inside the parent vm, you create the viewmodel that should stay behind the new view, and the service finds the associated one (based on some convention or registration) and then shows it.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-22 00:22

As Kiff noted:

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).

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.

You have to realize that M-V-VM is slightly different than other patterns like MVC or MVP. The ViewModel has no direct knowledge of the UI. Opening another view is a view-specific function. The View Model should care less what or how many views are using it's data. I most likely would never open the view via a command.

alt text http://blogs.msdn.com//johngossman/attachment/576163.ashx

查看更多
登录 后发表回答