由于您使用的是 MvvmLight,因此您可以使用 Messenger 类(mvvmlight 中的辅助类),用于在 ViewModel 之间以及 ViewModel 和 View 之间发送消息(通知 + 对象),在您登录成功的情况下LoginViewModel(可能在提交按钮的处理程序中)您需要向LoginWindow 发送消息以关闭自身并显示其他窗口:
LogInWindow 后面的代码
public partial class LogInWindow: Window
{
public LogInWindow()
{
InitializeComponent();
Closing += (s, e) => ViewModelLocator.Cleanup();
Messenger.Default.Register<NotificationMessage>(this, (message) =>
{
switch (message.Notification)
{
case "CloseWindow":
Messenger.Default.Send(new NotificationMessage("NewCourse"));
var otherWindow= new OtherWindowView();
otherWindow.Show();
this.Close();
break;
}
}
}
}
并在 SubmitButonCommandat LogInViewModel 中(例如)发送关闭消息:
private RelayCommand _submitButonCommand;
public RelayCommand SubmitButonCommand
{
get
{
return _closeWindowCommand
?? (_closeWindowCommand = new RelayCommand(
() => Messenger.Default.Send(new NotificationMessage("CloseWindow"))));
}
}
并使用相同的方法在 LoginViewModel 和 OtherWindowViewModel 之间发送对象,但这次您需要发送对象而不仅仅是 NotificationMessage :
在 LoginViwModel 中:
Messenger.Default.Send<YourObjectType>(new YourObjectType(), "Message");
并在OtherWindowViewModel 中接收该对象:
Messenger.Default.Register<YourObjectType>(this, "Message", (yourObjectType) =>
//use it
);