目前在我的应用程序中,我使用 func/lambda 方法显示消息框,如下面的网址所述:
http://www.deanchalk.me.uk/post/WPF-MVVM-e28093-Simple-e28098MessageBoxShowe28099-With-Action-Func.aspx
要传递消息框文本和标题是不是问题,但是我还想传递图像框图像和图像框类型(是/否等)。这些是 WPF 枚举。目前,我编写了一些方法将这些枚举转换为非 WPF(自己制作的)枚举,但复制每个值感觉有点乏味。
在 ViewModel 中使用 WPF 枚举是否可以接受? (我想不是)。如果没有,我怎样才能阻止它们被使用并仍然在 ViewModel 中选择它们?
Currently in my app I use the func/lambda method of showing message boxes as explained in the url below:
http://www.deanchalk.me.uk/post/WPF-MVVM-e28093-Simple-e28098MessageBoxShowe28099-With-Action-Func.aspx
To pass the message box text and caption is not a problem, however I also want to pass the image box image and image box type (yes/no etc). These are WPF enumerations. Currently I wrote a few methods to convert those enums into non WPF (own made) enums but it feels a bit tedious to copy every value.
Is it acceptable to use WPF enumerations in ViewModel? (I guess not). And if not, how can I prevent them to be used and still select them in the ViewModel?
发布评论
评论(1)
我对你的术语 ModelView 和 ViewModel 有点困惑。对于 MVVM,只有模型、视图和视图模型。
那篇文章讨论了抽象消息框,以便您可以运行单元测试,而不会在构建服务器等待用户交互时阻塞它。
该实现使用
Func
委托,但您可以使用接口轻松完成此操作。一种方法是创建您自己的枚举,然后将它们转换为接口的 MessageBox 实现。例如,
您的视图模型将采用 IConfirmation 作为依赖项(例如在其构造函数中),并且在单元测试中,您可以对 IConfirmation 接口进行存根以始终从 ShowConfirmation 方法返回特定结果。
您还可以重载 ShowConfirmation 方法来提供图像、窗口标题等选项。
I'm slightly confused with your terms ModelView, and ViewModel. With MVVM, there is just the model, the view, and the view model.
That article is talking about abstracting the message box so that you can run unit tests without blocking the build server while it waits for user interaction.
The implementation uses the
Func
delegate, but you could do this just as easily using an interface. An approach then would be to create your own enumerations, and then convert them for the MessageBox implementation of the interface.E.g.
Your view models would then take an IConfirmation as a dependency (in their constructor for example), and in unit tests, you can stub the IConfirmation interface to always return a particular result from the ShowConfirmation method.
You could also overload the ShowConfirmation method to provide options for images, window titles etc.