开发者

Childwindows in MVVM

开发者 https://www.devze.com 2023-01-03 15:17 出处:网络
I\'m having a problem understanding something about MVVM. My application relies on dialogs for certain things. The question is, where should these childwindows originat开发者_如何学运维e from? Accordi

I'm having a problem understanding something about MVVM. My application relies on dialogs for certain things. The question is, where should these childwindows originat开发者_如何学运维e from? According to MVVM, viewmodels should contain only businesslogic and have zero actual knowledge about UI. However, what other place should I call my childwindows from, considering they're UI elements?

Doesn't this create tight coupling between elements?


Since you tagged the question with Prism, I'll suggest the way I've done it in the past using Prism. Have the IEventAggregator injected into your ViewModel, and then when you want to pop open the dialog, publish a "ShowDialogEvent" or something like that. Then, have another Module called "DialogModule" or whatever, which upon initialization subscribes to that event, and shows the dialog. Furthermore, if you want to pass data back to the original ViewModel, have the ViewModel of the dialog publish a "DialogCloseEvent" or something like that with a payload of the data you need. You can then subscribe to that event back in your main ViewModel.


See Handling Dialogs in WPF with MVVM


In the past, I have accomplished this by using Unity to resolve a custom interface that has a Show() method and a completed event. Then in the ViewModel I would call IScreen screen = container.Resolve<IScreen>(Resources.EditorWindowKey); and then just call screen.Show();.

The big advantage of this is that I can then just simply change my Unity configuration to remove the view when I'm testing my VM's.


The primary route I've been using to do this is to create a command inside your View layer. That command object accepts a parameter that is the ViewModel object that you want to display. The command then finds the appropriate ChildWindow, creates it and displays it with the parameter set as the content or however you will set it up. This way you can just bind a button's command property to that command, and its commandparameter to the object you want to show in the popup and your ViewModel objects never have to care how it's being displayed.

Prompting for user input (like saving a dirty file or something) doesn't work in this scheme. But for simple popups where you manipulate some data and then move on, this works very well.


The ViewModel sample application of the WPF Application Framework (WAF) demonstrates how to show a Modal Dialog.


I would suggest to use a controller in this scenario, say DI'ed dialogController backed up with a dialog shell. The source viewmodel(ie from where the request to open a dialog is originating) will make a call to dialogController.ShowDialog(<<ViewNameToHostInRegion>>,<<RegionName>>).

In Order to transfer the data to and from the dialog and sourceview you can use MessageBus. So essentially when you invoke the ShowDialog() you populate the messagebus, and when the close command of target View(The view hosted in Dialog shell) invoked - say in "Select" button -- Let the target view add/update the messagebus. So that source view model can work with updated data.

It has got many advantages :

1) Your source view works with dialog controller as BlackBox. ie it doesnt aware of what exactly the Dialog view is doing.

2) The view is being hosted in Dialog Shell -- so you can reuse the dialog again and again

3) Unit testing of source view is limited to test the actual functionality of the current viewmodel, and not to test the dialog view\view model.

One heads-up which I came across with this one is, while creating the test cases you may need to write testable Dialog controller which do not show the actual dialog while running the testcases in bunch. So you will need to write a TestableDialogController in which ShowDialog does nothing (Apart from deriving from IDialogController and provide blank implementation of ShowDialog().

Following is the psudeo code :

LocalMessageBus.AddMessage(<MessageKey>,<MessageActualContentAsObject>);
dialogController.ShowDialog(<TargetViewName_SayEmployeeList>);
Employee selectedEmployee = LocalMessageBus.GetMessage(<MessageKey>) as Employee;
if (selectedEmployee != null)
{
//doSomework with selected employee
}
0

精彩评论

暂无评论...
验证码 换一张
取 消