How to implement interactive architecture in MVVM

I am developing a WPF 4.0 - MVVM application based on the PRISM (Unity Container) infrastructure.

I was wondering what is the best way to implement dialogs in the mvvm template. I plan to use quite a lot in my application, so I want something reusable.

+6
source share
3 answers

Since you are using Prism / Unity, implement a mediation template for your viewing models.

  • Add the DialogService module (IDialogService) to your project.
  • Modules containing dialogs register them using IDialogService. Remember to declare DialogServiceModule as module independent.
  • ViewModels now use IDialogService to display the desired dialog.

    public interface IDialogService { void RegisterDialog (string dialogID, Type type); bool? ShowDialog (string dialogID); } public class DialogService : IDialogService { private IUnityContainer m_unityContainer; private DialogServiceRegistry m_dialogServiceRegistry; public DialogService(IUnityContainer unityContainer) { m_unityContainer = unityContainer; m_dialogServiceRegistry = new DialogServiceRegistry(); } public void RegisterDialog(string dialogID, Type type) { m_dialogServiceRegistry.RegisterDialog(dialogID, type); } public bool? ShowDialog(string dialogID) { Type type = m_dialogServiceRegistry[dialogID]; Window window = m_unityContainer.Resolve(type) as Window; bool? dialogResult = window.ShowDialog(); return dialogResult; } } 

If you use ViewModel events and handlers in a view, use the WeakEventHandler pattern to eliminate potential resource leaks. In addition, multiple views can be attached to the same ViewModel. I worked on projects with one ViewModel -> one view. But also one ViewModel -> multiple views. Just something to consider when making design decisions.

+1
source

This article about the MVVM dialog box may come in handy: http://www.codeproject.com/Articles/36745/Showing-Dialogs-When-Using-the-MVVM-Pattern

+2
source

I let ViewModel create events when it needs to get user information. Then, before the idea of ​​how to put it. This means that the code behind the file will receive event handlers, but something real MVVM adherents will shudder ...

+1
source

All Articles