为什么选择MVVM及其核心优势是什么? [英] Why MVVM and what are it's core benefits?

查看:91
本文介绍了为什么选择MVVM及其核心优势是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在处理WPF时我们选择通过MVC或MVP进行MVVM?

Why we go for MVVM over MVC or MVP while dealing with WPF?

通过使用此功能,我们还能获得什么额外好处?

What extra benefit we get by using this?

说实话,今天我接受了采访,有人问我这个问题.我像INotifyPropertyChanged,ICommand,IValue Convertor ..那样回答,但他不满意.从今以后我提出了这个问题

To be honest , today I had an interview and I have been asked this question. I answered like INotifyPropertyChanged , ICommand,IValue Convertor.. but he was not satisfied. Henceforth I have put up this question

预先感谢

推荐答案

我将为您指出一个特别有用的视频.

I'll point you to a particularly useful video by Jason Dolinger.

来自WinForms的世界,实现任何MVX样式模式似乎都比其所需要的麻烦.但是,在与WPF合作了几年之后,我可以坦白地说,我不会再考虑什么了.开箱即用地支持整个范例.

Coming from a WinForms world, implementing any MVX style pattern seemed like more hassle than it was worth but after working with WPF for a couple of years now, I can honestly say that I wouldn't consider anything less. The whole paradigm is supported out-of-the-box.

首先,主要好处是可以实现viewmodel之间的真正分离.实际上,这意味着如果/当您的模型需要更改时,它可以不需要视图,反之亦然.

First off, the key benefit is enabling true separation between the view and model. What that means in real terms is that if/when your model needs to change, it can without the view needing to and vice-versa.

第二,虽然model可能包含view中可能需要的所有数据,但是您可能希望以model不支持的方式抽象该数据.例如,假设您的模型包含date属性.在模型中,它可以单独作为DateTime对象存在,但是您的视图可能希望以完全不同的方式呈现它.没有viewmodel,您将不得不复制model中的属性以支持视图,或者修改可能严重混淆模型"的属性.

Secondly, while your model may contain all the data you might need in your view, you may want to abstract that data in such a way that your model doesn't support. For example, say your model contains a date property. In the model it can exist solely as a DateTime object but your view might want to present it in a completely different way. Without the viewmodel you'd either have to duplicate the property in the model to support the view or modify the property which could seriously obfuscate the 'model'.

您还可以使用viewmodel来聚合模型中存在于单独的类/库中的部分,以便为view处理提供更流畅的界面. 非常,您不太可能希望以用户希望或希望将数据呈现给他们的相同方式来处理代码中的数据.

You can also use a viewmodel to aggregate parts of your model that exist in separate classes/libraries to facilitate a more fluent interface for the view to deal with. It's very unlikely that you'll want to work with data in your code in the same way that a user will want to or will want that data presented to them.

最重要的是,您还支持viewviewmodel之间的自动双向数据绑定.

On top of that, you get support for automatic two-way data binding between the view and viewmodel.

确实还有很多我可以借鉴的东西,但Jason说我可以,所以我的建议是观看视频.经过几天的工作,您会想知道如果没有它,您将如何过日子.

There really is a whole bunch of extra stuff that I could bang on about but Jason say's it far better that I could so my advice is watch the video. After a few days of working like this, you'll wonder how you ever got by without it.

祝你好运.

这篇关于为什么选择MVVM及其核心优势是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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