简体   繁体   English

好的Silverlight-MVVM实践示例

[英]Good Silverlight-MVVM Practice Example

I've read a number of good articles about the Model-View-ViewModel pattern and my team intends to implement this pattern in the latest version of our app. 我已经阅读了很多关于Model-View-ViewModel模式的好文章,我的团队打算在最新版本的应用程序中实现这种模式。 I still don't quite get ALL the bits that go together to make this work. 我仍然没有完全得到所有的位来使这项工作。 I'd like to find a good example of this pattern that I can work through. 我想找到一个我可以解决这个模式的好例子。 Something on a small scale, much like Jason Dolinger does in his video here , but I'd like something that I can work through on my own at a slower pace. 一些小规模很像贾森Dolinger在他的视频确实在这里 ,但我想的东西,我可以通过我自己以较慢的速度工作。

Thanks in advance for your help. 在此先感谢您的帮助。

Basically a ViewModel is a wrapper around model. 基本上,ViewModel是模型的包装器。 Now that might not be very helpful yet :-) Think of the model as the data your application works with, say a person. 现在这可能还不是很有用:-)把模型想象成你的应用程序使用的数据,比如说一个人。 Now a person has a birthday and you might want to have a form to enter the person's birthday. 现在一个人有一个生日,你可能想要一个表格来输入这个人的生日。 Suppose we are just using a simple TextBox the date shows up something like 01-02-2009 12:00:00AM. 假设我们只是使用一个简单的TextBox,日期会显示01-02-2009 12:00:00 AM之类的内容。 For starters we don't want the time part and we also might not be to happy about the 01-02-2009 part as this depends on your locale settings. 对于初学者我们不希望时间部分,我们也可能不会对01-02-2009部分感到高兴,因为这取决于您的语言环境设置。

So here the ViewModel comes im. 所以这里的ViewModel来了。 It wraps the Person class and exposed the date as three integer values, year, month and day. 它包装Person类并将日期暴露为三个整数值,年,月和日。 In the property set it tries to build a date from the different values and displays any errors that might occur. 在属性集中,它尝试从不同的值构建日期并显示可能发生的任何错误。

So simply said a ViewModel is a Model wrapper specifically geared towards a particular view (display). 所以简单地说,ViewModel是一个专门针对特定视图(显示)的Model包装器。 It eliminates most IValueConvertors at the same time. 它同时消除了大多数IValueConvertors。

Josh Smith has a nice explanation here: http://joshsmithonwpf.wordpress.com/2008/11/14/using-a-viewmodel-to-provide-meaningful-validation-error-messages and a big discussion here: http://groups.google.com/group/wpf-disciples/browse_thread/thread/3fe270cd107f184f?pli=1 Josh Smith在这里有一个很好的解释: http//joshsmithonwpf.wordpress.com/2008/11/14/using-a-viewmodel-to-provide-meaningful-validation-error-messages并在这里进行大讨论: http:/ /groups.google.com/group/wpf-disciples/browse_thread/thread/3fe270cd107f184f?pli=1

Prism 4.0 now includes documentation for creating MVVM applications. Prism 4.0现在包含用于创建MVVM应用程序的文档。 This is targeted for using the Prism libraries but the concepts work for the MVVM pattern in general. 这是针对使用Prism库的,但这些概念通常适用于MVVM模式。

http://compositewpf.codeplex.com/releases http://compositewpf.codeplex.com/releases

Here's one more to add to the list: Simple MVVM Toolkit by Tony Sneed (yours truly): http://simplemvvmtoolkit.codeplex.com . 这里还有一个要添加到列表中的内容:Tony Sneed的简单MVVM工具包(您真正的): http//simplemvvmtoolkit.codeplex.com

The toolkit consists of helper classes, Visual Studio item templates, and code snippets. 该工具包由辅助类,Visual Studio项模板和代码片段组成。 In addition to the actual toolkit, there is a sample app with step-by-step instructions on how to build a basic Silverlight app using the MVVM pattern. 除了实际的工具包之外,还有一个示例应用程序,其中包含有关如何使用MVVM模式构建基本Silverlight应用程序的分步说明。

I have gone through many recently, the two that stand out are: 我最近经历了很多,突出的两个是:

Dan Wahlin's (LIDNUG): Dan Wahlin(LIDNUG):

http://www.lidnug.org/Archives.aspx http://www.lidnug.org/Archives.aspx

John Papa's (PDC 2010): John Papa(PDC 2010):

http://player.microsoftpdc.com/Session/76864d75-b4da-4858-aac7-786c5f28e344 http://player.microsoftpdc.com/Session/76864d75-b4da-4858-aac7-786c5f28e344

Nikhil Kothari has a post about how to use that pattern with his Silverlight.FX library. Nikhil Kothari有一篇关于如何在他的Silverlight.FX库中使用该模式的帖子。 Check it out here: http://www.nikhilk.net/ViewModel-Pattern-DLR.aspx 请在此处查看: http//www.nikhilk.net/ViewModel-Pattern-DLR.aspx

The best explanation of a ViewModel was given by Scott Hanselman during his MVC talk in Europe. 对于ViewModel的最佳解释是由Scott Hanselman在欧洲的MVC演讲中给出的。

A one-liner response is -- It is a model (class) that is created for the sole purpose of sending all the necessary data that a particular View needs and also for receiving modified data from the View in case of two-way bindings. 单线响应是 - 它是一个模型(类),其创建的唯一目的是发送特定View所需的所有必要数据,以及在双向绑定的情况下从View接收修改数据。

Take a look at my article about real life MVVM in Silverlight. 看看我在Silverlight中关于真实MVVM的文章。 Though it is not for newcomers, and targeting mostly expert developers working on real life projects. 虽然它不适合新手,并且主要针对从事现实生活项目的专业开发人员。

http://alexburtsev.wordpress.com/2011/03/05/mvvm-pattern-in-silverlight-and-wpf/ http://alexburtsev.wordpress.com/2011/03/05/mvvm-pattern-in-silverlight-and-wpf/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM