Close window through the ViewModel
<p>I have a login window that appears in the following scenarios:</p>

<ul>
<li>Application starts: if the user has not logged in ( client id that is stored in the Properties.Setting ), the login window appears. The user logs in, the login windows closes and the main opens upon success.</li>
<li>The user is already logged in, but now wants to log in as a different user: the user presses a the login button and the login window appears and the user logs in. Upon success, the login window closes, and  the new user's information  is displayed in a small textbox.</li>
</ul>

<p>The problem here is that I want to use the same login window in both scenarios. What I imagined was a custom callback method for each of these scenarios that would be invoked upon logon success. I'm facing the problem of accessing LogonWindow through my LogonViewModel, where the logic of logging in happens. The reason for this, is that I want to close the window upon success and do other stuff.</p>

<p>Here's my code:</p>

<p><strong>App.xaml.cs</strong></p>

<pre><code>private void check()
{
    if (isloggedin)
    {
        Client c = new Client();
        Main m = new Main(c);
        m.Show(); //
    }
    else
    {
        LoginViewModel vm = new LoginViewModel();
        vm.callback = (loginViewModel, client) =&gt;
        {
            // the user logged in, now show the window and close the login window
            Main m = new Main(client);
            m.Show();

            // close LoginWindow after we're done logging in. 
            // But where? Who has access to window?
        };
        LoginWindow lw = new LoginWindow(vm);
        lw.Show();
    }
}
</code></pre>

<p><strong>LoginViewModel.cs</strong></p>

<pre><code>public class LoginViewModel : ViewModelBase
{
    public Action&lt;LoginViewModel, Client&gt; callback { get; set; }

    public LoginViewModel()
    {
    }

    public ICommand SignIn
    {
        get
        {
            return new RelayCommand&lt;object&gt;(signin, o =&gt; canLogin());
        }
    }

    private bool canLogin()
    {
        return true;
    }

    private void signin(object o)
    {
        if (clogin.login())
        {
            Console.WriteLine("Client Found!");
            Client client = clogin.getClient();
            // save settings
            Properties.Settings.Default.clientid = client.Uid;
            Properties.Settings.Default.Save();
            Client = client;
            // pass the new client, and pass the current view model?

            callback(this, client);
        }
    }
}
</code></pre>

<p>The initial thought was the to pass the entire window object to the viewmodel, but I think that's breaking essence of MVVM and the be able to Close the window from within my callback method since I'm already passing in the <code>LoginViewModel</code>. My question, is there a way to achieve what I'm trying to do? Remember, the essence is to do something after the user's logged in successfully depending from which point in my application... Thanks for the input.</p>