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

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

问题描述

为什么我们在处理 WPF 时选择 MVVM 而不是 MVC 或 MVP?

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 中可能需要的所有数据,但您可能希望以这样的方式抽象该数据,以便您的 模型 不支持.例如,假设您的模型包含日期属性.在模型中,它可以单独作为 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.

确实有一大堆额外的东西我可以说,但杰森说我可以更好,所以我的建议是观看视频.像这样工作几天后,你会想知道没有它你是怎么过的.

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