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 loo开发者_Python百科sely coupled.
Does this sound like a sensible thing to do? Is there a better way? Am I missing something?
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: Open File Dialog MVVM
精彩评论