某處是否存在名爲NotificationMessageWithCallback的MvvmLight功能的WPF示例?WPF-MvvmLight NotificationMessageWithCallback示例?
我只想問一個簡單的刪除確認對話框。
感謝
某處是否存在名爲NotificationMessageWithCallback的MvvmLight功能的WPF示例?WPF-MvvmLight NotificationMessageWithCallback示例?
我只想問一個簡單的刪除確認對話框。
感謝
要從視圖模型的值傳遞到視圖,首先創建一個自定義Message
與相關屬性。我們從NotificationMessageAction<MessageBoxResult>
繼承你提到你想要一個確認框
public class MyMessage : NotificationMessageAction<MessageBoxResult>
{
public string MyProperty { get; set; }
public MyMessage(object sender, string notification, Action<MessageBoxResult> callback) :
base (sender, notification, callback)
{
}
}
在我們的視圖模型我的命令發送新MyMessage
被擊中(SomeCommand
)
public class MyViewModel : ViewModelBase
{
public RelayCommand SomeCommand
{
get
{
return new RelayCommand(() =>
{
var msg = new MyMessage(this, "Delete", (result) =>
{
//result holds the users input from delete dialog box
if (result == MessageBoxResult.Ok)
{
//delete from viewmodel
}
}) { MyProperty = "some value to pass to view" };
//send the message
Messenger.Default.Send(msg);
}
});
}
}
}
最後,我們需要在註冊的消息後面的視圖代碼
public partial class MainWindow : Window
{
private string myOtherProperty;
public MainWindow()
{
InitializeComponent();
Messenger.Default.Register<MyMessage>(this, (msg) =>
{
myOtherProperty = msg.MyProperty;
var result = MessageBox.Show("Are you sure you want to delete?", "Delete", MessageBoxButton.OKCancel);
msg.Execute(result);
}
效果很好。感謝這個簡單的例子。 – emoreau99 2015-03-17 14:22:58
我可能是在錯誤的軌道上。看來,我可以簡單地使用NotificationMessageAction [鏈接](http://stackoverflow.com/questions/6440492/how-to-receive-dialogresult-using-mvvm-light-messenger)。 我可以得到它的工作。還有一個問題:如何傳遞參數?例如,我的ViewModel如何在發送MessageBox中顯示的消息時傳遞一個值? – emoreau99 2015-03-13 13:48:45
在您提到的鏈接中,您將向ShowPasswordMessage添加一個屬性。 – SWilko 2015-03-16 08:55:11
虛擬機將如何通過NotificationMessageAction向視圖提供值? – emoreau99 2015-03-16 18:13:27