Dialog box for configuration in MVVM

I am in the new MVVM. My current problem is the modular dialog box, which should start with "autorun" at the beginning.

I followed the WAF mail client example for modular dialogs. Is it right that you need to set the owner property of the dialog box to an instance of the main application window (and, of course, show the window with ShowDialog () instead of Show ()?

If you close this dialog box without configuration, the application will shut down. But now, if I open the main window in the designer mode of visual studios, the configuration dialog will appear, and if I close it, the visual studio will crash. This is because I call ShowDialog () on the configuration dialog in the constructor of my main window view model.

To avoid this, I can check the DesignerProperties.IsInDesignTool property, but this is more of a workaround as a good code style, right?

Do you have any suggestions? Thanks.

+4
source share
1 answer

The problem is that you are displaying a dialog in the class constructor. This is what you do not want to do.
I would solve it like this:
Do not specify StartupUri in app.xaml, but override OnStartup . There you check whether the configuration dialog should be displayed or not. If it should be shown, show it, and after closing it with OK, display the main window.

Something like that:

 override void OnStartup(...) { if(configurationNotComplete) { ConfigDialog cfg = new ConfigDialog(); if(!(cfg.ShowDialog() ?? false)) { Shutdown(); return; } } MainWindow window = new MainWindow(); window.Show(); } 

You have another problem with your current approach: your ViewModel shows a modal dialog. This means that he knows at least about one kind: this is a modal dialogue. MVVM is one way: View knows about the ViewModel, ViewModel knows about the model. There should be no connection in the other direction.

+4
source

All Articles