Jonas Van der Aa
Jonas Van der Aa

Reputation: 1461

How do I navigate from one view to another in Caliburn.Micro?

So let me put it this way.

I have a LogInViewModel and a LogInView. There is a Login() method in the ViewModel that gets called if the user clicks on a button in the View. Now I want the dashboard to show if the login was successful. How do I do this? I can't find a clear answer to this in the documentation.

Upvotes: 1

Views: 1872

Answers (2)

Jens Pettersson
Jens Pettersson

Reputation: 1177

I've just added a very simple login example SL4 project in my "lab repository" for Caliburn.Micro.

https://github.com/jenspettersson/Caliburn.Micro.Labs/tree/master/src/Login

It uses the Show class that Rob Eisenberg uses in his "Game Library" example to switch between views.

In the Login() method, it tells my Shell (your dashboard?) to show my LoginResultViewModel and sets the login result message.

yield return Show.Child<LoginResultViewModel>().In<IShell>().Configured(c => c.ResultMessage = "Successfully logged in!");

Check the code in my github repo.

I havent used Caliburn.Micro very much lately, so I am by no means an expert, but this way works for me.

//J

Edit: This answers how to navigate between views, if you want to show a "popup" to display if the login was successful, go with the other recomendations.

Upvotes: 2

devdigital
devdigital

Reputation: 34349

I assume that your dashboard is essentially your shell. In which case, you can bootstrap your LoginViewModel and in the Login method, after a successful login, you can show the DashboardViewModel and close the LoginViewModel using the Caliburn.Micro WindowManager.

Something like (using MEF):

Bootstrapper.cs

public class Bootstrapper : Caliburn.Micro.Bootstrapper<ILoginViewModel>
{
  ...
}

LoginViewModel.cs

public class LoginViewModel : Screen, ILoginViewModel
{
    private readonly IWindowManager windowManager;

    private readonly IDashboardViewModel dashboardViewModel;

    [ImportingConstructor]
    public LoginViewModel(IWindowManager windowManager, IDashboardViewModel dashboardViewModel)
    {
        this.windowManager = windowManager;
        this.dashboardViewModel = dashboardViewModel;
    }

    public void Login()
    {            
        // if login success...
        this.windowManager.ShowDialog(this.dashboardViewModel);
        this.TryClose();
    }
}

Upvotes: 3

Related Questions