MVVM疯狂:命令 [英] MVVM Madness: Commands

查看:139
本文介绍了MVVM疯狂:命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢MVVM。我不喜欢它,但喜欢它。大多数情况是有道理的。但是,我继续阅读文章,鼓励您编写大量代码,以便您可以编写XAML,而不必在代码隐藏中编写任何代码。



让我举个例子。



最近我想把ViewModel中的命令连接到ListView MouseDoubleClickEvent。我不太清楚如何做到这一点。幸运的是,Google有一切答案。我发现以下文章:





虽然解决方案有助于我的理解的命令,有问题。上述一些解决方案使WPF设计器不可用,因为在依赖属性后面附加了一个内部 WPF设计器找不到它,但CLR可以。一些解决方案不允许多个命令到同一个控件。一些解决方案不允许参数。



经过几个小时的实验,我只是决定这样做:

  private void ListView_MouseDoubleClick(object sender,MouseButtonEventArgs e){
ListView lv = sender as ListView;
MyViewModel vm = this.DataContext作为MyViewModel;

vm.DoSomethingCommand.Execute(lv.SelectedItem);
}

所以,MVVM纯粹主义者,请告诉我这是什么问题?我可以单位测试我的命令。这似乎很实用,但是似乎违反了ZOMG ...你在代码隐藏代码中的代码!的指南。请分享您的想法。



提前感谢

解决方案

认为错误在于纯度要求。设计模式,包括MVVM,是工具箱中的一个工具,而不是结束自己。如果将一个考虑严重的案例的模型的纯度打破更有意义(看起来好像已经考虑过这种情况),然后再使用模型。



如果这适用于你,而你不相信这是一个不正当的维护负担,那么我会说你做了什么都没有错。我认为您已经清楚地遇到了证明责任,证明这是您的问题的合理解决方案,尽管纯粹的MVVM实现可能是。



我认为这个论点类似于multiparadigm语言的论点,虽然可以应用纯OO方法,但有时以更有效的方式做事情更合适,虽然可以应用Pure Functional方法,但是有时权衡表明OO技术是值得的。)


I like MVVM. I don't love it, but like it. Most of it makes sense. But, I keep reading articles that encourage you to write a lot of code so that you can write XAML and don't have to write any code in the code-behind.

Let me give you an example.

Recently I wanted to hookup a command in my ViewModel to a ListView MouseDoubleClickEvent. I wasn't quite sure how to do this. Fortunately, Google has answers for everything. I found the following articles:

While the solutions were helpful in my understanding of commands, there were problems. Some of the aforementioned solutions rendered the WPF designer unusable because of a common hack of appending "Internal" after a dependency property; the WPF designer can't find it, but the CLR can. Some of the solutions didn't allow multiple commands to the same control. Some of the solutions didn't allow parameters.

After experimenting for a few hours I just decided to do this:

private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
    ListView lv = sender as ListView;
    MyViewModel vm = this.DataContext as MyViewModel;

    vm.DoSomethingCommand.Execute(lv.SelectedItem);
}

So, MVVM purists, please tell me what's wrong with this? I can still Unit test my command. This seems very practical, but seems to violate the guideline of "ZOMG... you have code in your code-behind!!!!" Please share your thoughts.

Thanks in advance.

解决方案

I think the fault lies in the purity requirement. Design patterns, MVVM included, are a tool in the toolbox, not an end unto themselves. If it makes more sense to break with the purity of the model for a well-considered case (and it clearly looks like you've considered this case), then break with the model.

If that works for you, and you don't believe it's an undue maintenance burden, then I'd say that nothing is wrong with what you've done. I think that you've clearly met the burden of proof for showing that this is a reasonable solution to your problem in spite of what a pure MVVM implementation might be.

(I consider this argument similar to the arguments for multiparadigm languages. While a Pure OO approach can be applied, sometimes doing things in a more functional way is more appropriate. While a Pure Functional approach can be applied, sometimes the trade offs show that OO techniques are more than worth the while.)

这篇关于MVVM疯狂:命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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