One sentence explanation to MVVM in WPF?

2019-01-12 20:36发布

I heard its the next best thing in building WPF UIs, but all existing examples have dozens of lines of code - can I get a Hello World for MVVM that explains in no uncertain terms what its all about? I'm fairly new to C#/.net as well, so maybe point me to some resources that could help too?

Much appreciated!

11条回答
迷人小祖宗
2楼-- · 2019-01-12 21:11

The MVVM pattern is when the UI interfaces with an xaml-friendly intermediate object to get at your xaml-unfriendly actual data.

查看更多
虎瘦雄心在
3楼-- · 2019-01-12 21:15

a pattern where the frontend(view) and backend(modal) communicates (back and forth) using a common mediator(view-modal).

查看更多
我只想做你的唯一
4楼-- · 2019-01-12 21:17

This site has a great diagram that explains it.

Basically you have 3 components:
1) Model - The data model of your application. this is pretty standard and the same as any MVP or MVC app.
2) View - The XAML that defines the view/layout of your app.
3) View Model - Because WPF demands that the view attach to things in certain ways (like requires that collections implement INotifyCollectionChanged and stuff like that) it normally requires that you massage your data a little bit to get it in a form suitable for the view to display. This is where the view model codes in. It packages up the data into view models that the view can easily display. This is what your view XAML will bind to. It must respond to events from the model layer and update itself.

(Then your controllers hang on the side somewhere - ideally using WPF commands - and make changes to the model, which fires events to update the view model)

查看更多
狗以群分
5楼-- · 2019-01-12 21:20

Because you can't data-bind to your own codebehind

(only half joking here)

查看更多
SAY GOODBYE
6楼-- · 2019-01-12 21:23

The simple statement that helped me get my head around it best was "Could I unit test my business logic without the user interface?" I think this should be the question you ask while learning and designing using MVVM concepts.

查看更多
登录 后发表回答