Kelly
Kelly

Reputation: 7193

Calling another ViewModel using Caliburn.micro

I have two views call them A and B. When a user performs a specific action on A, I want it to launch B. Both views should remain open.

A inherits Conductor and I've tried to call ActivateItem but view B never opens.

What am I missing? Is this is correct way to call one view from another using Caliburn.micro?

Upvotes: 2

Views: 1576

Answers (1)

Patryk Ćwiek
Patryk Ćwiek

Reputation: 14328

OK, here's the easiest solution if the B-view is supposed to be in a different window. I'm assuming that AViewModel is the 'shell' view model:

public class AViewModel // : Screen - that's optional right here ;)
{
    private readonly BViewModel bViewModel;
    private readonly IWindowManager windowManager;

    public AViewModel(BViewModel bViewModel, IWindowManager windowManager)
    {
        this.bViewModel = bViewModel;
        this.windowManager = windowManager;
    }

    public void ShowB()
    {
        windowManager.ShowWindow(bViewModel); //If you want a modal dialog, then use ShowDialog that returns a bool?
    }
}

and the simplest of views (AView):

<Button Name="ShowB" Content="Show another window"/>

It will show the new window with BView when you click a button on AView.

Conductors are used to have multiple screens in one window (or, in different words, multiple panes on one screen). Sometimes it's OneActive (e.g. when implementing navigation in WPF) or AllActive when there are multiple screens available at once.

Here's a nice tutorial regarding some of the Caliburn.Micro's basics and that particular part describes the Window Manager.

Upvotes: 4

Related Questions