在我使用 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中获取已发布的消息?
谢谢你的回复。