只需将消息从/传递到 ViewModel。
查看:
private void Window_Closing(object sender, CancelEventArgs e)
{
Messenger.Default.Send(new WindowRequestsClosingMessage(
this,
null,
result =>
{
if (!result)
e.Cancel = true;
});
}
视图模型:
Messenger.Default.Register<WindowRequestsClosingMessage>(
this,
msg =>
{
// Your logic before close
if (CanClose)
msg.Execute(true);
else
msg.Execute(false);
});
留言:
public class WindowRequestsClosingMessage: NotificationMessageAction<bool>
{
public WindowRequestsClosingMessage(string notification, Action<bool> callback)
: base(notification, callback)
{
}
public WindowRequestsClosingMessage(object sender, string notification, Action<bool> callback)
: base(sender, notification, callback)
{
}
public WindowRequestsClosingMessage(object sender, object target, string notification, Action<bool> callback)
: base(sender, target, notification, callback)
{
}
}
MVVM Light 的NotificationMessageAction<TResult>允许您传递消息并获取 TResult 类型的结果。要将 TResult 传递回请求者,请Execute()
像示例一样调用。