如何防止ModelView中的WPF枚举 [英] How to prevent WPF enums in ModelView

查看:70
本文介绍了如何防止ModelView中的WPF枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,在我的应用中,我使用func/lambda方法显示消息框,如以下网址中所述:

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

传递消息框文本和标题不是问题,但是我也想传递图像框图像和图像框类型(是/否等).这些是WPF枚举.目前,我编写了一些方法将这些枚举转换为非WPF(自己制作的)枚举,但是复制每个值都有些繁琐.

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.

在ViewModel中使用WPF枚举是否可以接受? (我猜不是).如果没有,如何防止它们被使用而仍然在ViewModel中选择它们?

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?

推荐答案

我对您的术语ModelView和ViewModel感到有些困惑.使用MVVM,只有模型,视图和视图模型.

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.

该实现使用Func委托,但是您可以使用接口轻松地完成此任务.然后,一种方法是创建自己的枚举,然后将其转换为接口的MessageBox实现.

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.

例如

public enum ConfirmationResult
{
  Yes,
  No, 
  Cancel
  ..etc
}

public enum ConfirmationType
{
  YesNo,
  OkCancel
  ..etc    
}

public interface IConfirmation
{
  ConfirmationResult ShowConfirmation(string message, ConfirmationType confirmationType)
}

public class MessageBoxConfirmation : IConfirmation
{
  ConfirmationResult ShowConfirmation(string message, ConfirmationType confirmationType)
  {
    // convert ConfirmationType into MessageBox type here
    // MessageBox.Show(...)
    // convert result to ConfirmationResult type
  }
}

您的视图模型随后将IConfirmation作为依赖项(例如,在其构造函数中),并且在单元测试中,可以对IConfirmation接口进行存根以始终从ShowConfirmation方法返回特定结果.

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.

您还可以重载ShowConfirmation方法以提供图像,窗口标题等选项.

You could also overload the ShowConfirmation method to provide options for images, window titles etc.

这篇关于如何防止ModelView中的WPF枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆