2017-03-11 31 views
0

當我們使用MVVM時,我們被告知要避免在我們的ViewModel中使用System.Windows.MessageBox,我想這是因爲它不會對我們的測試有好處。這是真的嗎?Prism NotificationRequest vs Xceed MessageBox

使用Prism NotificationRequest,我們可以與我們的用戶交流,但它比簡單的MessageBox稍微複雜一些。

另一種方法是使用Xceed Wpf Toolkit MessageBox,它比Prism NotificationRequest簡單。

我的問題是:他們都是等同的嗎?我們可以使用MVVM方式嗎?如果否,我們什麼時候需要使用NotificationRequest,什麼時候可以使用Xceed MessageBox?

謝謝

回答

0

如果從可與模擬測試時被替換服務呼叫MessageBox.Show(),你的罰款。

畢竟,你不想要的是一個消息框運行您的視圖模型單元測試時彈出...

例子:

public interface IMessageBoxService 
{ 
    ClickedButten ShowMessageBox(string message, Buttons buttons); 
} 

internal class SomeViewModel 
{ 
    public SomeViewModel(IMessageBoxService messageBoxService) 
    { 
     _messageBoxService = messageBoxService; 
    } 

    public void SomeMethodThatNeedsAMessageBox() 
    { 
     var theClickedButton = _messageBoxService.ShowMessageBox("Click me!", Buttons.Ok | Buttons.Cancel); 
     // react to the click... 
    } 
} 

internal class SystemMessageBoxService : IMessageBoxService 
{ 
    public ClickedButten ShowMessageBox(string message, Buttons buttons) 
    { 
     // adapt parameters... 
     MessageBox.Show(...); 
     // adapt result... 
    } 
} 

internal class XceedMessageBoxService : IMessageBoxService 
{ 
    public ClickedButten ShowMessageBox(string message, Buttons buttons) 
    { 
     // adapt parameters... 
     Xceed.ShowMessageBox(...); 
     // adapt result... 
    } 
} 

現在只需綁定所需的服務使用(甚至可以在運行時決定),並在測試時注入模擬。