简体   繁体   English

Caliburn.Micro从UserControl调用视图模型

[英]Caliburn.Micro Calling a viewmodel from UserControl

I have a AppViewModel, that contains a menu on Top of the Window. 我有一个AppViewModel,它在窗口顶部包含一个菜单。 On the AppViewModel construct, I'm showing a UserControl. 在AppViewModel构造上,我正在显示一个UserControl。 In this UserControl I have a button, that calls another viewmodel (UserControl). 在此UserControl中,我有一个按钮,它调用另一个视图模型(UserControl)。

The idea is to keep the menu and working on the content of window. 这个想法是保留菜单并处理窗口的内容。 So, I have 1 window and 2 UserControls. 因此,我有1个窗口和2个UserControls。 This is correct? 这是对的?

How can I call another ViewModel from a button that is inside of a UserControl? 如何从UserControl内部的按钮调用另一个ViewModel? Or, I have to call it from the Window? 或者,我必须从窗口调用它? But the button it's inside of the UserControl! 但是按钮在UserControl内部!

My code: 我的代码:

class AppViewModel : Conductor<object>
{
    private bool _MenuIsVisible;

    public bool MenuIsVisible
    {
        get { return _MenuIsVisible; }
        set
        {
            if (_MenuIsVisible != value)
            {
                _MenuIsVisible = value;
                NotifyOfPropertyChange(() => MenuIsVisible);
            }
        }
    }

    public AppViewModel()
    {
        MenuIsVisible = true;
        _ShowTutorial();
    }

    private void _ShowTutorial()
    {
        ActivateItem(new FirstViewModel());
    }

}



public class FirstViewModel : Screen
{
    protected override void OnActivate()
    {
        base.OnActivate();
    }
}

On the FirstViewModel I have a button that needs to call SecondViewModel. 在FirstViewModel上,我有一个需要调用SecondViewModel的按钮。

To navigate from the first ViewModel to the second ViewModel you could have a method in the first ViewModel like this: 要从第一个ViewModel导航到第二个ViewModel,您可以在第一个ViewModel中使用如下方法:

public void NavigateToSecond()
{
    var conductor = this.Parent as IConductor;
    conductor.ActivateItem(new SecondViewModel());
}

The parent refers to the conductor which will take care of navigating for you. 父母是指将为您导航的售票员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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