0

在我使用 Caliburn.micro 框架学习 MVVM 之后......我正在尝试像这样通过 EventAggregator 通信两个视图模型发送数据(“没有意义”的代码,仅用于测试):

MainWindowViewModel.cs

namespace TOP
{
[Export(typeof(MainWindowViewModel))]
public class MainWindowViewModel : Conductor<IScreen>.Collection.OneActive
{
    readonly IWindowManager windowManager;
    private readonly IEventAggregator events;
    private bool _Logged;

    [ImportingConstructor]
    public MainWindowViewModel(IWindowManager windowManager, IEventAggregator events)
    {
        DisplayName = "TOP";
        this.events = events;
        events.Subscribe(this);
        this.windowManager = windowManager;
        windowManager.ShowDialog(new LoginViewModel(events));

    }

    public bool Logged
    {
        get { return _Logged; }
        set
        {
            _Logged = value;
            if(_Logged== true) 
                InitiateApp();
        }
    }

    public void Handle(LoginEvent message)
    {
        Logged = message.Logged;
    }

    private void InitiateApp() {
        ActivateItem(new TwoWindowViewModel());
        }
    }
}

登录视图模型.cs

namespace TOP{ 

 [Export(typeof(IScreen))]
    public class LoginViewModel : Screen
    {
        private readonly IEventAggregator _events;

        [ImportingConstructor]
        public LoginViewModel(IEventAggregator events)
        {

           DisplayName = "Login";
           _events = events;
           Login();
        }


       public void Login()
       {
           _events.Publish(new LoginEvent(true));
       }
    }
}

登录事件.cs

    namespace TOP
    {
        public class LoginEvent
        {
           public LoginEvent(bool logged) 
           {
               Logged = logged;
           }

         public bool Logged { get; private set; }
    }
}

为什么MainWindowViewModel的Handle方法没有从LoginViewModel中获取已发布的消息?

谢谢你的回复。

4

1 回答 1

3

MainWindowViewModel需要实施IHandle<LoginEvent>. 您已经使用正确的签名定义了该方法,因此您只缺少实际告诉编译器您实现接口的部分:

public class MainWindowViewModel
    : Conductor<IScreen>.Collection.OneActive, IHandle<LoginEvent>

更多信息可以在文档中找到。

于 2013-09-24T06:25:51.583 回答