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.
Steven licht
source share