NotificationMessageWithCallback 的 WPF-MvvmLight 示例?

WPF-MvvmLight sample of NotificationMessageWithCallback?

某处是否有名为 Notification Message WithCallback 的 Mvvm Light 功能的 WPF 示例?

我只想问一个简单的删除确认对话框。

谢谢

要将值从 ViewModel 传递到 View,首先创建自定义 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)
    {
    }
}

在我们的 ViewModel 中,我发送了一个新的 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);
           }