Implementing “close window” command with MVVM

后端 未结 12 1706
既然无缘
既然无缘 2020-12-05 09:31

So my first attempt did everything out of the code behind, and now I\'m trying to refactor my code to use the MVVM pattern, following the guidance of the MVVM in the box inf

12条回答
  •  感动是毒
    2020-12-05 10:03

    MVVM-light with a custom message notification to avoid the window to process every notificationmessage

    In the viewmodel:

    public class CloseDialogMessage : NotificationMessage
    {
        public CloseDialogMessage(object sender) : base(sender, "") { }
    }
    
    private void OnClose()
    {
        Messenger.Default.Send(new CloseDialogMessage(this));
    }
    

    Register the message in the window constructor:

    Messenger.Default.Register(this, nm =>
    {
        Close();
    });
    

提交回复
热议问题