剛剛從/到視圖模型傳遞消息。
查看:
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燈的NotificationMessageAction <TResult>允許您傳遞消息並獲得TResult類型的結果。要將TResult傳遞迴請求者,請像示例一樣呼叫Execute()
。
謝謝Jota。我會檢查並回復你。 – Dennis 2013-02-21 09:58:41