How have you successfully implemented MessageBox.Show() functionality in MVVM?

后端 未结 12 1533
清歌不尽
清歌不尽 2020-11-28 05:55

I\'ve got a WPF application which calls MessageBox.Show() way back in the ViewModel (to check if the user really wants to delete). This actually works

12条回答
  •  半阙折子戏
    2020-11-28 06:29

    Services to the rescue. Using Onyx (disclaimer, I'm the author) this is as easy as:

    public void Foo()
    {
        IDisplayMessage dm = this.View.GetService();
        dm.Show("Hello, world!");
    }
    

    In a running application, this will indirectly call MessageBox.Show("Hello, world!"). When testing, the IDisplayMessage service can be mocked and provided to the ViewModel to do what ever you want to accomplish during the test.

提交回复
热议问题