Implementing “close window” command with MVVM

后端 未结 12 1704
既然无缘
既然无缘 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:17

    You don't need to pass the View instance to your ViewModel layer. You can access the main window like this -

    Application.Current.MainWindow.Close()
    

    I see no issue in accessing your main window in ViewModel class as stated above. As per MVVM principle there should not be tight coupling between your View and ViewModel i.e. they should work be oblivious of others operation. Here, we are not passing anything to ViewModel from View. If you want to look for other options this might help you - Close window using MVVM

提交回复
热议问题