Just pass messages from / to the ViewModel.
View
private void Window_Closing(object sender, CancelEventArgs e) { Messenger.Default.Send(new WindowRequestsClosingMessage( this, null, result => { if (!result) e.Cancel = true; }); }
ViewModel
Messenger.Default.Register<WindowRequestsClosingMessage>( this, msg => {
Message
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> allows you to send a message and get a result of type TResult. To pass TResult back to the requestor, call Execute() exactly the same way as in the example.
source share