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

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

问题描述

我喜欢MVVM。我不喜欢它,但喜欢它。大多数的很有道理。但是,我继续读,鼓励你写了很多code,这样你可以写XAML和没有写任何code在code-后面的文章。

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.

让我给你举个例子。

最近我想在我的视图模型到ListView MouseDoubleClickEvent联播的命令。我不太知道如何做到这一点。幸运的是,谷歌有答案的一切。我发现下面的文章:

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:

  • <一个href="http://blog.functionalfun.net/2008/09/hooking-up-commands-to-events-in-wpf.html">http://blog.functionalfun.net/2008/09/hooking-up-commands-to-events-in-wpf.html
  • <一个href="http://joyfulwpf.blogspot.com/2009/05/mvvm-invoking-command-on-attached-event.html">http://joyfulwpf.blogspot.com/2009/05/mvvm-invoking-command-on-attached-event.html
  • http://sachabarber.net/?p=514
  • <一个href="http://geekswithblogs.net/HouseOfBilz/archive/2009/08/27/adventures-in-mvvm-ndash-binding-commands-to-any-event.aspx">http://geekswithblogs.net/HouseOfBilz/archive/2009/08/27/adventures-in-mvvm-ndash-binding-commands-to-any-event.aspx
  • <一个href="http://marlongrech.word$p$pss.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/">http://marlongrech.word$p$pss.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/
  • http://blog.functionalfun.net/2008/09/hooking-up-commands-to-events-in-wpf.html
  • http://joyfulwpf.blogspot.com/2009/05/mvvm-invoking-command-on-attached-event.html
  • http://sachabarber.net/?p=514
  • http://geekswithblogs.net/HouseOfBilz/archive/2009/08/27/adventures-in-mvvm-ndash-binding-commands-to-any-event.aspx
  • http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/

虽然解决方案都有助于我的命令的理解,有问题。一些上述解决方案的渲染,因为附加的内部一个依赖属性后,一个共同的黑客WPF设计器无法使用; WPF设计无法找到它,但CLR可以。一些解决方案,没有允许多个命令到相同的控制。一些解决方案的不容许参数

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);
}

所以,MVVM较真的,请告诉我有什么错呢?但我还是能单位测试我的命令。这似乎很实用,但似乎违背的准则ZOMG ......你有code在code-后面!!!!请分享你的想法。

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.

在此先感谢。

推荐答案

我认为故障出在纯度要求。设计模式,MVVM在内,都在工具箱中的工具,而不是你们自己结束。如果它更有意义的模型一个考虑周全的情况下的纯度突破(这显然看起来像你认为这种情况下),然后用该模型打破。

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.

如果为你的作品,你不相信这是一个不必要的维护负担,那么我会说,没有什么是错你做了什么。我认为你已经清楚地满足举证责任显示,这是一个合理的解决您的问题,尽管什么是纯MVVM的实现可能。

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天全站免登陆