Tell me how to show the form modally, taking into account MVVM? Those. ordinary ShowDialog does not fit explicitly, because does not fit into the pattern.
1 answer
For example:
For the dialog box, you must also have a view model. In the place where you want to show the dialogue, create an instance of this view model.
Then there may be two options:
but. Using a special service. You have some
IDialogServicein which you can register a view / view model correspondence at the initialization stage of the application, as well as trigger a dialog showing by passing the desired view model. TheShowDialog()method returns values ​​similar to theWindow.ShowDialog()method. A specific instance ofIDialogServiceis transferred to the requiredIDialogServicemodels from the outside (if you use DI) or simply created at the place of need (which is worse).public interface IDialogService { void RegisterDialog<TView, TViewModel>(); bool? ShowDialog<TViewModel>(TViewModel viewModel); }An example implementation can be seen in the answer VladD .
b. Using messages. You have some message 'ShowDialogMessage', which you pass to the messenger. Either one window (the “main”) or specific “parent” windows subscribe to this message. Depends on the desired degree of detail. If you need to get a "return value", then in the message you just need to add the appropriate field, which will be filled in the place where the message is processed.
public class ShowDialogMessage<TViewModel>: MessageBase { public TViewModel ViewModel { get; private set; } public SomeType Result { get; set; } public ShowDialogMessage(TViewModel viewModel) { ViewModel = viewModel; } }Regarding the places where specific dialog boxes are created and displayed. Their implementation may differ depending on what you need. If the dialog box (and, accordingly, its view model) is generic (for example, it's just a messaging box, with a title, text, and type), then this could be one place for the entire application. If the dialog boxes are unique, then there will be several places to show them - in place on the "type" of the window. Sometimes this can be reduced to one place, if you use the dialog / view model map (see the
RegisterDialog()method in the first method).
- but. As it is not very clear. What kind of interface? Where is the class that implements it? Where should it be created? b. It looks like something simpler here, but the main question here is that it throws an event, and the code will go on. Those. when the modal windows are shown, the code at the place of the call ShowDialog stops and waits for the window to close, and in this case (if I understood correctly) the event will simply roll and the code will continue without waiting for the form to close. - PECHAPPER
- @DarkByte he wrote typical approaches. Well, classes should be implemented for personal needs. in the case of sending an alert, the code will not go on until they notify and handle the subscribers' handlers, and if the subscriber has ShowDialog, then it will stop there until the dialog closes. - vitidev
- Well, the truth is the first example is not at all clear! Please can be an example with the implementation? And according to the second example, yes, it really works as planned, but what if you have to show different dialog boxes in different places? How to filter them? Those. as an instant messenger I took mvvm light, there is ready. But there I’m letting you just send the message 0 and, by the int type, I catch it in the MainWindow and display the dialog. And what if there are many dialog boxes? Do not use the same type for each ... As it smacks of govnokodom. - PECHAPTER
- And by the way in the second example, what about the return of values? Well, for example, if I showed not just a dialog box, but a MessageBox with buttons and I need to mean which button was pressed. - PECHAIR
- one@maxwell where you are processing the message. Because it is more likely that this view will be "internal" for the place of processing the message. It is also possible to have hybrid options - when there is a service dialogue and messages (both sub-paragraph 2). - andreycha