How to Display Messages at Application Startup

May 16, 2013 at 3:53 PM
Hi,

I am trying to port an application to CM in order to really get my head around CM usage. So far, things are going pretty good and I am extremely impressed / pleased with CM. I have run into a situation, and I am not sure how to handle it.

My application does several checks when it loads to determine if it can in fact run (can I connect to the web service? Is the user valid? Is the user running an acceptable version?) and if anything fails, displays a message to the user why it can't run and shuts down. So I have (with all cruft removed) a ShellViewModel and in the VM's constructor, I check these things.
public ShellViewModel()
{
    if (!CanLogIn())
    {
        // display message (using my own singleton window manager)
        WindowManager.Default.OpenDialog(new DialogMessageViewModel{ Message = "Can't open!" });

        // would normally call this, but for some reason with CM, not required
        //Application.Current.Shutdown();
    }
}
This works well, it displays my message before the main window is displayed. However, a side effect that I can't figure out, is that after the window manager displays the DialogMessageViewModal, the user clicks "OK", even though I have the Application.Current.Shutdown() commented out, the application still shuts down rather than carrying on and displaying the ShellViewModel / ShellView.

This is actually my desired behavior most of the time, however, not always:
  1. In the case of an invalid version, I need to give the user the option to check for updates (via another DialogMessageVIewModel display.
  2. The application also checks for available updates and if the version they are running is acceptable, but there is an update available, I present them with a "Update now?" dialog. If they click NO, that's fine, they can still carry on using the application.
So my questions are:
  1. Why does displaying a secondary viewmodel with the window manager in the constructor of my shell viewmodel cause the application to exit when the secondary viewmodel is closed?
  2. What is a good procedure to display these startup messages with my current intent?
My Bootstrapper has a bunch of stuff in it, but I also get the same results if I comment out everything and go with the most basic bootstrapper possible.

Thanks!
Scott
May 16, 2013 at 4:26 PM
Try changing the application shutdown mode to explicit. Otherwise the application is shutdown if the las window is closed.
May 16, 2013 at 7:50 PM
Thanks, that got me on the right track. I determined there are two ways to handle this.
  1. Leave Application.Current.ShutDownMode as/is and change it on the fly in my Shell view model if I'm doing a function that will need to NOT close the application when a dialog is dismissed. Then be sure to flip it back when the function is done. Messy, but if your app doesn't have a "main" window that you can say "when this closes the application life cycle is done", it works well.
  2. Set Application.Current.ShutDownMode to OnExplicitShutdown in App.xaml. Override OnDeactivate(bool close) in the Shell view model and call Application.Shutdown() is "close" == true.
I opted for #2 in my case, as the Shell view model really is the central shell, when it closes, you have no more application.