Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf Prism NotificationRequest vs Xceed MessageBox_Wpf_Mvvm_Prism_Xceed_Prism 6 - Fatal编程技术网

Wpf Prism NotificationRequest vs Xceed MessageBox

Wpf Prism NotificationRequest vs Xceed MessageBox,wpf,mvvm,prism,xceed,prism-6,Wpf,Mvvm,Prism,Xceed,Prism 6,当我们使用MVVM时,我们被告知要避免在ViewModel中使用System.Windows.MessageBox,我想这是因为这对我们的测试不好。这是真的吗 使用Prism NotificationRequest,我们可以与用户通信,但它比简单的MessageBox稍微复杂一些 另一种方法是使用Xceed Wpf Toolkit MessageBox,它比Prism NotificationRequest更简单 我的问题是:两者是否相等?我们能以MVVM的方式使用它们中的任何一个吗?如果没有,

当我们使用MVVM时,我们被告知要避免在ViewModel中使用System.Windows.MessageBox,我想这是因为这对我们的测试不好。这是真的吗

使用Prism NotificationRequest,我们可以与用户通信,但它比简单的MessageBox稍微复杂一些

另一种方法是使用Xceed Wpf Toolkit MessageBox,它比Prism NotificationRequest更简单

我的问题是:两者是否相等?我们能以MVVM的方式使用它们中的任何一个吗?如果没有,我们什么时候需要使用NotificationRequest,什么时候可以使用Xceed MessageBox


感谢您

如果您在测试时可以用模拟替换的服务中调用
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...
    }
}
现在只需绑定您想要使用的服务(甚至可以在运行时决定),并在测试时注入一个mock