2

There is an interesting post discussing communication patterns between view models.

I recently struggled to implement a modal dialog in a WPF MVVM application, but there more I think about it the more I see reasons for why it's difficult and why there's no built-in helpers to achieve this. Targeting different platforms only strengthen this view: what may look suitable for a modal dialog showing selected item details is usually implemented by navigating to a different view on mobile devices.

So my questions to those who successfully implemented MVVM pattern and avoided code-behind: did you also avoid modal dialogs? What replacements have you found suitable? I can think of at least two:

  • Place child (modal) view in a new view and implement communication between parent and sub-model using pub/sub;
  • Add a panel with a child control directly to a parent view and just turn its visibility to activate child view as a popup simulating modality.

The second approach is surely more limited, it does not really work when the parent view can spawn various child views, but it looks suitable when the parent view needs to display a small single popup. Or is it better to go for the first approach as the more generic. Are there other alternatives?

Community
  • 1
  • 1
Vagif Abilov
  • 9,835
  • 8
  • 55
  • 100

1 Answers1

1

thats the way i do dialogs in mvvm.

ViewModel which calls a dialog window:

 var result = this.uiDialogService.ShowDialog("Dialogwindow title goes here", dialogwindowVM);

 ... do anything with the dialog result...
Community
  • 1
  • 1
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • Thanks, I've seen this approach. I am uncertain about it because then we treat dialogs like they were just services, so there is no big difference between displaying a dialog in such service and calling a Web service. In other words we take it out of shared MVVM space even though each individual dialog can be implemented using MVVM pattern. Another thing is that we declare that certain operation must use DialogService. What if it uses different means on different platforms? Then we can't reuse it. – Vagif Abilov Apr 24 '13 at 12:52
  • what you mean by reuse? reuse a .net viewmodel?? – blindmeis Apr 24 '13 at 13:35
  • Yes, I've seen view models packed in a portable class library and reused between WPF, Windows Store, Windows Phone and even iOS/Android applications. I would like to build my models in such a way, even though it will take some effort to figure out how. – Vagif Abilov Apr 24 '13 at 14:18
  • i see. you can reuse such service because its just an interface. i export/import the implementation with MEF in my projects. – blindmeis Apr 25 '13 at 05:32