简体   繁体   中英

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?

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM