简体   繁体   English

MVVM对Josh Smith的示例应用程序的疑问

[英]MVVM questions on Josh Smith's Sample Application

I have been working through Josh Smith's article on MVVM at http://msdn.microsoft.com/en-us/magazine/dd419663.aspx . 我一直在浏览Josh Smith关于MVVM的文章, 网址http://msdn.microsoft.com/en-us/magazine/dd419663.aspx Each section makes sense to me but I am having a hard time putting it all together as a coherent unit mentally. 每个部分对我来说都很有意义,但我很难将其作为一个连贯的单元在精神上进行整合。 I have 2 questions that would help a ton. 我有2个问题会帮助很多人。

  1. If I were to build the sample ap, what would be the logical order to build it? 如果我要构建示例ap,那么构建它的逻辑顺序是什么?

  2. For the command structure, what happens, in what order when the ap is run? 对于命令结构,以什么顺序运行ap会发生什么?

I am also wondering if I should split this into 2 questions? 我也想知道是否应该将此分为两个问题?

  1. I agree with Yacoder on this one. 我同意Yacoder在这一点上的看法。 Start with what you know, or your vision. 从您所知道的或您的愿景开始。 If your vision is to get a certain UX, start in Expression Blend if you want to. 如果您希望获得特定的UX,则可以从Expression Blend开始。 If you know what functionality you want, start with the ViewModels and the Unit tests. 如果知道所需的功能,请从ViewModels和Unit测试开始。

  2. Smith's application starts with App.xaml.cs. Smith的应用程序从App.xaml.cs开始。 There the MainWindowViewModel and the MainWIndow is created and shown. 在那里创建并显示了MainWindowViewModel和MainWIndow。

MainWindow.xaml is the next thing that happens. MainWindow.xaml是接下来发生的事情。 It defines the main portion of the UI. 它定义了UI的主要部分。 The main parts of this is showing two collections; 其中的主要部分显示了两个集合。 Commands and Workspaces. 命令和工作区。 Those are members of MainWindowViewModel. 这些是MainWindowViewModel的成员。

Smith seems to like properties to check if their corresponding private fields are null and, if they are, assign them. Smith似乎喜欢属性来检查其对应的私有字段是否为null,如果是,则将其分配。 Thus the "Commands" collection is created in line 51 of MainWindowViewModel which calls CreateCommands() just south of there. 因此,在MainWindowViewModel的第51行中创建了“ Commands”集合,该集合在该窗口的南部调用CreateCommands()。

The command classes are abstracted away by RelayCommand, probably because each command doesn't need to know much in the case of "Show All" or "Create". 命令类由RelayCommand抽象化,可能是因为在“显示全部”或“创建”的情况下,每个命令不需要了解太多。 The methods for these two commands are in the MainWindowViewModel, because they are conceptually functions of the main window. 这两个命令的方法在MainWindowViewModel中,因为它们在概念上是主窗口的功能。

The Commands collection is visualized as a list in the Main Window, so they need some kind of presentable, user friendly text to describe them. Commands集合在主窗口中显示为一个列表,因此它们需要某种可呈现的,用户友好的文本来描述它们。 Thus they are wrapped in their own CommandViewModels. 因此,它们被包装在自己的CommandViewModels中。

The commands are presented through the magic of XAML beginning at line 41 of MainWindow.xaml. 从MainWindow.xaml的第41行开始,通过XAML的魔力来显示命令。 The HeaderedContentControl is databound to the Commands collection, and specifies CommandsTemplate of MainWindowResources.xaml (starting at line 93 of that file). HeaderedContentControl数据绑定到Commands集合,并指定MainWindowResources.xaml的CommandsTemplate(从该文件的第93行开始)。 The template uses a HyperLink with its Command property bound to the Command property of the CommandViewModel. 该模板使用HyperLink,其Command属性绑定到CommandViewModel的Command属性。

When it comes to the Save button on the new customer form. 当涉及到新客户表单上的“保存”按钮时。 This is bound from CustomerView.xaml, line 117. To the CustomerViewModel SaveCommand property in line 196. It is a RelayCommand pointing to methods in CustomerViewModel. 它从CustomerView.xaml的第117行绑定到第196行的CustomerViewModel SaveCommand属性。它是一个RelayCommand,指向CustomerViewModel中的方法。 Each customer view has its own instance of CustomerViewModel where the data for that customer goes. 每个客户视图都有其自己的CustomerViewModel实例,该客户的数据将流向该实例。 The instances of RelayCommand belong to those CustomerViewModels, so each view has it's own SaveCommand also. RelayCommand的实例属于那些CustomerViewModels,因此每个视图也具有自己的SaveCommand。 The action and predicate of the RelayCommand instance knows not only which methods and properties they point to, but also of which instance. RelayCommand实例的操作和谓词不仅知道它们指向哪些方法和属性,还知道哪个实例。 The Save method of CustomerViewModel only uses data from that instance. CustomerViewModel的Save方法仅使用该实例中的数据。

That's roughly how two views can have the same kind of buttons that do the same for their respective customer data. 这大概就是两个视图可以具有对各自的客户数据执行相同操作的相同按钮的方式。

  1. Start with the part you know better. 从您更了解的部分开始。 UI or model that is. UI或模型。 Anyway you will have to do several iterations over the whole MVVM thing to make all parts fit together. 无论如何,您将必须对整个MVVM进行多次迭代,以使所有零件装配在一起。
  2. Your second question is not exactly clear for me, I'd say no commands are started just by the fact that application runs, some action, like button click triggers the command to do its action. 您的第二个问题对我来说还不是很清楚,我想说的是,仅凭应用程序运行,某些操作(例如单击按钮即可触发命令来执行命令)就不会启动任何命令。 Also, every command can be enabled/disabled, which is then reflected by the visual state of the corresponding control. 同样,每个命令都可以启用/禁用,然后由相应控件的可视状态反映出来。 It's quite a common pattern even out of the boundaries of the MVVM and WPF. 即使在MVVM和WPF的边界之外,这也是相当常见的模式

Josh Smith's article is the best resource on MVVM. Josh Smith的文章是有关MVVM的最佳资源。 But if you're unable to understand it straight away, you might consider reading mine which anyone can easily understand. 但是,如果您不能立即理解它,则可以考虑阅读任何人都可以轻松理解的内容。 http://codingtales.com/2010/02/06/creating-a-complete-tabbed-interface-in-wpf-using-mvvm/ http://codingtales.com/2010/02/06/creating-a-complete-tabbed-interface-in-wpf-using-mvvm/

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

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