Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM and commands that show more GUI

Tags:

mvvm

wpf

I like the MVVM idea of a RelayCommand that's exposed by the ViewModel. That's nice and elegant, for operations that can be done with no further user input. Simple. Testable.

However, not all operations are UI-less. Some require confirmation ("Are you sure you want to delete?"). Others require more information yet. Opening a file might entail anything from a File Open dialog up through a full-blown Import wizard.

What's the best way, within an MVVM app, to write commands that require user input? Is there an established pattern for solving this with dependency injection somehow? Should I write a KeyDown handler in the code-behind, and have it explicitly execute the event? Should I fall back on RoutedUICommand, and put all the "display the next GUI" code in my View? Or is there something obvious that I'm totally missing?

like image 252
Joe White Avatar asked Jun 21 '09 14:06

Joe White


1 Answers

I usually use Dependency Injection to inject some kind of abstract IShowTheInterface thing, and then invoke methods on the abstraction from within the Command. These methods should then give you the answers you need to determine whether to continue with the action at all, and what input the user gave.

I recently used this as an example in a blog post with a bit of a different topic.

like image 103
Mark Seemann Avatar answered Oct 29 '22 00:10

Mark Seemann