减少样板代码MVVM WPF应用程序的附加属性,命令等? [英] Reducing boilerplate code in MVVM WPF app for attached properties, commands, etc?

查看:289
本文介绍了减少样板代码MVVM WPF应用程序的附加属性,命令等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个WPF MVVM应用。那我注意到的事情是,我必须写的样板代码了过多刚(从WPF团队的MVVM工具包通过DelegateCommands)宣布命令,附加属性和附加的行为。是否有可用来减少我写的样板代码量的策略是什么?

I'm working on a WPF MVVM application. The thing that I'm noticing is that I have to write an inordinate amount of boilerplate code just to declare commands (through DelegateCommands from the WPF Team's MVVM Toolkit), attached properties and attached behaviors. Are there any strategies available to reduce the amount of boilerplate code I have to write?

谢谢!

推荐答案

就个人而言,我喜欢约什 - 史密斯的 MVVM基业库。他使用了一个名为 RelayCommand 有类,但它的声音从工具包非常相似,你的 DelegateCommand 。它可以让你创建一个命令并通过的 CanExecute 的和的执行的通过lambda表达式逻辑。这将有助于减少大量的样板代码。

Personally, I like Josh Smith's MVVM Foundation library. He uses a class called RelayCommand there, but it sounds pretty similar to your DelegateCommand from the toolkit. It lets you create a command and pass the CanExecute and Execute logic through lambda expressions. That will help reduce a lot of boilerplate code.

他的博客,乔希还谈到了使用通用属性观察员避免一些的PropertyChanged 事件处理的混乱方面。这是值得研究的,也是如此。

In his blog, Josh also talks about using a generic property observer to avoid some of the messier aspects of PropertyChanged event handling. That is worth looking into, as well.

老实说,虽然,很多所谓的样板代码设置为您的应用一个非常动态的,灵活的基础。如果你正在一个小的,易于维护的应用程序,你可能会问自己,的我甚至不需要适用于此MVVM模式?的如果,另一方面,你这是一个更大的应用程序将具有寿命长,并且需要大量的维护工作,那么这个样板代码是要保存下来就行了。

Honestly though, a lot of the so-called "boilerplate" code is setting up a very dynamic and flexible foundation for your application. If you are making a small, easily maintained application, you might ask yourself, "do I even need to apply the MVVM pattern here?" If, on the other hand, you are making a larger application that will have a long life-time and require a lot of maintenance, then this boilerplate code is going to save you down the line.

这篇关于减少样板代码MVVM WPF应用程序的附加属性,命令等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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