One sentence explanation to MVVM in WPF?

前端 未结 11 1177
有刺的猬
有刺的猬 2020-12-12 13:13

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

相关标签:
11条回答
  • 2020-12-12 13:36

    Some really good one-sentence (or close to it) explanations already. I'll just add to it by saying that if you're past the basic level and you want an example of how to use MVVM in a real-world app that has menus, tool bar, options dialogs, docking tools windows, etc., take a look at SoapBox Core and this demo using SoapBox Core. It is open sourced so you can get lots of ideas.

    0 讨论(0)
  • 2020-12-12 13:40

    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)

    0 讨论(0)
  • 2020-12-12 13:44

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

    0 讨论(0)
  • 2020-12-12 13:44

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

    (only half joking here)

    0 讨论(0)
  • 2020-12-12 13:46

    MVVM is a star-fan relationship. The fan knows the star but the star does not know the fan. The fan loves his star so much that if the star changes himself ( I mean his dressing style ), the fan changes himself accordingly.

    Now replace "star" with "ViewModel" and "fan" with "View" and read it once again.

    0 讨论(0)
  • 2020-12-12 13:48

    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.

    0 讨论(0)
提交回复
热议问题