views:

239

answers:

1

I am writing an application using Microsoft's Prism framework and I'm having trouble working out the best way to approach a simple 'Save File' style dialog box.
NB: I can't use the standard dialog as I'm not saving files to the file system so need to write something similar from scratch.

So here are the main controls I have for my dialog:

  • A user control that shows the hierarchy of folders. (IFolderView)
  • A list view to show the contents of a folder. (IFolderContentsView)
  • A text box for the user to enter the saved files name.

The only real interaction on the form is that when a user selects a folder on the left, the list view on the right gets populated with the folders contents.

When I started writing the dialog I originally created a couple of Regions for my user controls and used view discovery to add my views. I did this because its how I was injecting IFolderView elsewhere in the application. I now find that I need to know something about the controls in the region for them to interact with each other. I thought about the EventAggregator but that doesn't seam right for a simple save dialog.

My solution to this problem is to use view injection. So I ask my Unity container for an implementation of the IFolderViewModel and set its view as the contents of the region during initialization. That way I know enough about the user controls on the form to make it work, but its still all loosely coupled.

Does this sound like a sensible thing to do? Is there a better way? Am I missing something?

A: 

Most of the time with Prism, people are using an ISystemInteraction (Something like ISystemCommands, or similar) to raise dialogs or interact with the filesystem in some way. This way it remains testable, but the ViewModel isn't responsible for doing anything view-specific. Here's an example interface:

public interface ISystemCommands
{
     //Raises a save file dialog with a prepopulated name
     void SaveFile(string name, byte[] content);
}

Then you would provide an implementation to be made available from the Shell that would talk to the filesystem. This is both MVVM and testable (which I like).

There is a related question here that basically gives the same answer: http://stackoverflow.com/questions/1043918/open-file-dialog-mvvm/1044304#1044304

Anderson Imes
Thanks for you reply, but it isn't really what I was looking for. My problem is how I get two controls to talk to each other in a simple dialog when all I know about them is what region they're in. Your reply was still useful to me though, thanks!
Jon Mitchell
@JMitchell: that description is much clearer. For getting two open views (and thus their viewmodels) to talk to each other, you can use event aggregator. However, if you need two-way communication, that would likely not be appropriate. In this case, you would use the same interface I proposed, but make the ViewModel of the target view implement it, rather than a central service to the Shell.
Anderson Imes