Standard Approach for Starting Windows Dialogs / Children from a WPF Application Using MVVM

That's all, I would like to know the recognized best approach / industry standard for launching [child] dialogs / windows from WPF using the MVVM template. I came across the following articles:

but. CodeProject - display dialogs when using the MVVM template

This approach seems good, but excessive for me. This is some degree of code replication, and I'm not sure if this is the right way.

B. WPF MVVM and mapping dialogs

This briefly goes through three options with different links, which are pretty / very bad at explaining the methodology or are topics.

Can someone explain the explanation of the standard method / approach for launching dialogs from a WPF application using MVVM and preferably some links to additional reading materials? If you can give an example yourself, I would certainly be very grateful!

Thank you for your time.

+7
source share
6 answers

First of all, I do not know a single “industry standard” for showing dialogs using MVVM, because this does not exist.
Secondly, Welcome to MVVM, you have just touched upon areas in which MVVM does not have a standard. In truth, MVVMs have a lot of pain points, and for that very reason there are tons of MVVM frameworks, just to mention a few MVVM Light, PRISM, Caliburn.Micro, Cinch, Catel, WAF, Baboon, shell I stop or you want more.
Now, to answer your question and having examined most of these frameworks, I noticed one commonality: they all use the DI / IoC container and then provide you with an interface, something like IDialogManager and their own implementation, and then they ask you to accept this interface in your view model and use it to display dialogs. Therefore, to summarize, I would use dependency injection, have an interface to display dialogs, and then provide and implement this, and register it with the di container, and then consume it from my model or views.
Edit: So, you chose PRISM (which, in my opinion), the hardest thing to show is the dialogs. Now, aside, there is a difficult way, which is to use “Interaction Requests” (check the middle of the article), or you can use this Answer as a faster way.

+7
source

I recently implemented my own navigation service for WPF, which uses Caliburn.Micro WindowManager (but you can replace it with something else).

Example (how to use):

_navigationService.GetWindow<ClientDetailsViewModel>() .WithParam(vm => vm.IsEditing, true) .WithParam(vm => vm.Client, SelectedClient) .DoIfSuccess(() => RefreshSelectedClient()) .ShowWindowModal(); 

Implementation:

 namespace ClientApplication.Utilities { public class NavigationService : INavigationService { SimpleContainer _container; IWindowManager _windowManager; public NavigationService(SimpleContainer container, IWindowManager windowManager) { _container = container; _windowManager = windowManager; } public INavigationService<TViewModel> GetWindow<TViewModel>() { return new NavigationService<TViewModel>(_windowManager, (TViewModel)_container.GetInstance(typeof(TViewModel), null)); } } public class NavigationService<TVM> : INavigationService<TVM> { IWindowManager _windowManager; TVM _viewModel; System.Action _action; public NavigationService(IWindowManager windowManager, TVM viewModel) { _windowManager = windowManager; _viewModel = viewModel; } public INavigationService<TVM> WithParam<TProperty>(Expression<Func<TVM, TProperty>> property, TProperty value) { var prop = (PropertyInfo)((MemberExpression)property.Body).Member; prop.SetValue(_viewModel, value, null); return this; } public INavigationService<TVM> DoBeforeShow(Action<TVM> action) { action(_viewModel); return this; } public INavigationService<TVM> DoIfSuccess(System.Action action) { _action = action; return this; } public void ShowWindow(IDictionary<string, object> settings = null) { _windowManager.ShowWindow(_viewModel, null, settings); } public bool ShowWindowModal(IDictionary<string, object> settings = null) { bool result = _windowManager.ShowDialog(_viewModel, null, settings) ?? false; if (result && _action != null) _action(); return result; } } } 

Interfaces:

 namespace Common { public interface INavigationService<TVM> { INavigationService<TVM> WithParam<TProperty>(Expression<Func<TVM, TProperty>> property, TProperty value); INavigationService<TVM> DoIfSuccess(System.Action action); INavigationService<TVM> DoBeforeShow(Action<TVM> action); void ShowWindow(IDictionary<string, object> settings = null); bool ShowWindowModal(IDictionary<string, object> settings = null); } public interface INavigationService { INavigationService<TViewModel> GetWindow<TViewModel>(); } } 
+2
source

The latest release of Prism ( download here ) contains the so-called "Link Implementation" of the MVVM application called "Stock Trader". My explanation was that if the Prism team calls it the "Reference implementation", then the most "standard" from their point of view (if anything in MVVM is standard) and the logical choice to continue.

The source contains an infrastructure library for creating modal dialogs, which is pretty good. Therefore, I accepted this library and successfully deployed it (I downloaded such an application in Codeplex).

I needed to set the code to 1 to add the parent icon to the title bar because the library did not provide it; and [2] add some meaningful text to the title bar because the library left it empty and [3] add a delegate to call when the dialog closes. It abstracts to the extent that the virtual machine can raise the dialog by passing two lines (i.e. Unity login names) to the intermediary. These changes are available on Codeplex.

Thus, among all other “standards”, “Reference Implementation” should be minimally involved as a viable choice. A more inclined answer to your question is that if your viewing models are sufficiently isolated and fully work through POCO interfaces, then in THEORY this does not matter, because switching to another "standard" should be a trivial exercise.

+2
source

Creating a dialog box worked well for me, and is also suggested in both of your links.

Later I saw the same solution in the days of Dev in the presentation of MVVM by Gill Cleeren . Check out the link to working code samples (although written for Metro)

The only thing that hurts me a bit in the dialog service is that it somehow depends on the user interface (rich client).

A simple request and response web interface can be built on top of the same ViewModel and Model code that WPAM XAML is bound to. Until the ViewModel opens the dialogs using the dialog box. I do not know how to implement a dialog box for web view. Implementing dialogs there will require a more logical presentation.

+1
source

i just use dialogservice see here

in your view model, which you just have to do:

 var result = this.uiDialogService.ShowDialog("Dialogwindow title goes here", dialogwindowVM); ... do anything with the dialog result... 
+1
source

The purpose of using the interface to implement dialogs is to make the code testable. In this case, “A” is widely used, but it is still difficult to say “Standard”. If you do not have a test for ViewModel or you can verify that ViewModel does not affect dialogs, for example, using Extract-Override, you definitely cannot follow the instructions.

0
source

All Articles