Search code examples
c#.netwpf.net-coreprism

Logout and return to Login screen in Prism Application


I have a login sequence working in my prism based WPF application which shows the login screen before the main window is shown. in my Bootstrapper class, the InitializeShell method looks like this

    protected override void InitializeShell()
    {
        var logon = new LogOnView();
        bool? res = logon.ShowDialog();
        if (!res ?? true)
        {
            Application.Current.Shutdown(1);
        }
        else
        {
            Application.Current.MainWindow.Show();
        }
    }

and this works as expected, and is fine if the user logs in, uses the app and then closes it down.

What I now want to do is allow the user to logout, or force a logout pragmatically.

If I use code similar to the above in the MainWindowViewModel to try and kill the MainWindow and show a LogOnView that doesn't work.

It almost feels like I need some sort of Window Manager to show and hide the 2 screens which is initalized once the bootstrapper has done it's thing, or have the bootstrapper respond to an event from the MainWindow or something.

What is the best way to achieve this?


Solution

  • It almost feels like I need some sort of Window Manager to show and hide the 2 screens

    The default behavior is to show one shell and basically everything happens there. If you want more than one "shell" (e.g. a login window and a regular shell), you have to extend the code. A window manager that's called from InitializeShell sounds fine to me.

    // in the bootstrapper    
    protected override void InitializeShell()
    {
        Container.Resolve<IWindowManager>().SwitchToLoginWindow();
    }
    
    // from the login window
    private void OnCredentialsValidated()
    {
        _windowManager.SwitchToShell();
    }
    
    // somewhere else
    private void OnLogOut()
    {
        _windowManager.SwitchToLoginWindow();
    }
    

    The window manager creates and holds both the login window and the shell and shows one and hides the other. I'd try not to destroy and recreate the shell to prevent confusing things like the region manager.