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

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

public void Foo()
    IDisplayMessage dm = this.View.GetService<IDisplayMessage>();
    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.

Leave a Comment