The problem statement:
I am tired of thinking how to show a simple message box or user prompt and act based on the response in Model-View-View-Model (MVVM).
Common approaches:
- It's ok, let's just do this one thing from ViewModel and mock this out for unit testing!(https://geekswithblogs.net/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/wink_smile.gif)
- Design my own dialog, then what to do from there!(https://geekswithblogs.net/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/devil_smile.gif)
- Can I write something in view code behind, ah yes, that seems to be the only way out, as anyway MVVM is still not matured... !(https://geekswithblogs.net/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/whatchutalkingabout_smile.gif)
- and what not?
I am pretty much one among the few frustrated out in this world looking for some convincing answers. I think we can do it a little neater without having the feeling of violating any of our self defined rules!
Solution:
The Control
- Implement a simple control with no designer visibility.
!(/images/mukapu/user-prompts-messagebox-with-mvvm/70b54ed8-UserPrompt1.png)
- Allow a property to be bound to tell when to show the MessageBox
!(/images/mukapu/user-prompts-messagebox-with-mvvm/504a2e6d-UserPrompt2.png)
!(/images/mukapu/user-prompts-messagebox-with-mvvm/30006bbb-UserPrompt3.png)
- Provide command binding for possible user actions, Yes, No, Cancel...
!(/images/mukapu/user-prompts-messagebox-with-mvvm/5853efb6-UserPrompt4.png)
How do I Use?
- Just place the necessary XAML tag in the view
!(/images/mukapu/user-prompts-messagebox-with-mvvm/998c0712-UserPrompt5.png)
- Implement the command for all user actions in the View Model
!(/images/mukapu/user-prompts-messagebox-with-mvvm/1c8c410e-UserPrompt6.png)
- Run unit tests on the commands
!(/images/mukapu/user-prompts-messagebox-with-mvvm/b4af0bc2-UserPrompt7.png)
I am also attaching the code for the MessageBox control and a demo application.
Hope this is useful to somebody out there... Comments are welcome....
!(file:///C:/DOCUME1/706015/LOCALS1/Temp/moz-screenshot-1.png)
