VB WPF MVC(模型+视图+?)
我有一个旧的 VB6 应用程序。我想使用 WPF 在 VB.Net 中重新创建它。但我对“模型视图控制器”模式有点困惑。我有两本关于设计模式的书(GoF 和 J.Bishop),这两本书中确实没有提到这种模式。我还在互联网上搜索了一些java示例。但我仍然不知道如何在我的新 WPF 应用程序中使用 MVC 模式(应该吗?)。
举例来说,我的模型(实际上更复杂)只是一个轮辋(圆形),具有制造商、直径和深度属性。用户应该能够使用文本框和组合框修改属性。
有人可以创建一个小示例来解释 WPF 的 MVC 模式吗? 当然,我喜欢可重用的类,以便在整个应用程序中拥有一个可行的概念。
提前致谢 哎呀
I have an old VB6 application. I want to recreate it in VB.Net using WPF. But I am a bit confused about the "Model View Controller"-pattern. I have two books about design patterns (GoF and J.Bishop) afair this pattern is indeed not mentioned inside one of the two books. I have also searched the internet I found some java-examples. But I have still no clue how I should use MVC-Pattern (should I?) in my new WPF-application.
Let's say for example my model (in fact it is more complex) is only a wheel rim (circle) with the properties Manufacturer, Diameter and Depth. The user should be able to modify the properties using textboxes and ComboBoxes.
Could somebody create a small example that explaines the MVC-Pattern with WPF?
Of course I like reusable classes to have a feasible concept throughout the whole application.
thanks in advance
Oops
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是关于 MVC 模式是什么以及如何将其应用到 WPF 应用程序的“简要”描述。
(我可能有一些细节略有偏差,因为我主要是在 Silverlight 中进行黑客攻击,但概念足够相似。)
基本上,这个想法是分离关注点并定义应用程序不同部分之间的接口,目的是保持代码结构化且可维护。
您示例中的 Model 与您描述的轮辋几乎完全相同 - 一个 WheelRim 类,具有以合适的数据类型定义的各种属性。我会将模型放在一个单独的程序集中,以将其与其他代码分开,但您可以将模型类保留在“模型”文件夹中。该模型在数据库中还有一个“双胞胎”,模型类几乎一对一映射到表。
(您可能想看看 Linq2SQL 或实体框架,如果定义了数据库,您几乎可以免费获得模型以及合适的数据库访问代码。)
View 将是实际的 WPF xaml文件 - 定义网格或画布或您拥有的东西。在 WheelRimView 上,会有用于显示或访问不同属性的标签和文本框,可能还有产品图像等。视图后面的代码将包含所有相关的事件处理程序(start、button_click 等),用于从字段获取数据并将其传递到控制器。
控制器可以是您用来操作数据的任何“处理程序代码”。我们在这里讨论基本的 CRUD 操作,以及验证等。此外,控制器层将负责将数据编译为可以无缝进入视图的格式。因此,WheelRimController 将具有“GetWheelRimList”、“GetWheelRim”、“AddWheelRim”、“ModifyWheelRim”和“DeleteWheelRim”等方法。这些方法将值作为参数并相应地操作模型对象。我
建议保持 xaml 文件的代码隐藏不受任何“控制器”式代码的影响,例如验证、聚合等 - 后面的代码基本上应该只从文本框、列表框等中获取值并将它们发送到“按原样”到控制器方法进行处理。此外,在获取用于演示的数据时,您应该将任何数据格式化代码保持在最低限度(即,不在视图中进行过滤或转换)。
“用户打开轮辋并编辑直径”的典型用例将在代码中执行:
。
Here's a "brief" description of what the MVC pattern is and how I would apply it to a WPF application.
(I might have a few details slightly off since I've mainy hacked in Silverlight but the concept is similar enough.)
Basically, the idea is to separate concerns and define interfaces between the different parts of an application, with the goal of keeping the code structured and maintainable.
The Model in your example would be pretty much exactly as you described the wheel rim - a WheelRim class with the various properties defined in suitable data types. I would put the model i an separate assembly to keep it apart from the other code, but you can settle for just keeping the model classes in a "Models" folder. The model would also have a "twin" in a database, the model classes being pretty much one-to-one-mapped to tables.
(You might wanna have a look at Linq2SQL or Entity Framework, if the database is defined you can pretty much get the model for free along with suitable database access code.)
The View would be the actual WPF xaml files - Defining the Grid or Canvas or what have you. On the WheelRimView there would be labels and textboxes for displaying or accessing the different properties, perhaps along with product images and the like. The code behind for the view would have all the relevant event handlers (start, button_click and so on) for getting the data from the fields and passing them to the controllers.
The Controller would be any "handler code" that you would use to manipulate the data. We're talking the basic CRUD operations here, along with validation and the like. Also, the controller layer would be responsible for compiling the data in a format that can go seamlessly into the View. The WheelRimController hence would have methods like "GetWheelRimList", "GetWheelRim", "AddWheelRim", "ModifyWheelRim" and "DeleteWheelRim". The methods take the values as in parameters and manipulate the model objects accordingliy. the
I would recommend keeping the code-behind of the xaml files free from any "controller"-ish code like validation, aggregation and the like - the code behind should basically only take the values from the textboxes, listboxes and such and send them on "as is" to the controller methods for processing. Also, you should keep any data formatting code to a minimum when getting data for presentation (i.e., no filtering or translating in the view).
A typical use case of "User opens a wheel rim and edits the diameter" would play out thus in code:
I hope that clears a few bits up.
由于提供了丰富的绑定支持,WPF(和 Silverlight)非常适合 MVVM(模型-视图-视图模型)。 MVVM 是 MVC 的扩展,它使用视图模型来绑定视图的当前状态,而不是直接操作视图。
有很多可用的 MVVM 框架,以及 Microsoft 自己的 Prism 框架(如果您有更大的模块化应用程序)。
Bevcause of the rich binding support available, WPF (and Silverlight) are well suited to MVVM (Model-View-ViewModel). MVVM is an extension of MVC that uses a view model to bind the current state of a view, instead of manipulating the view directly.
There are a bunch of MVVM frameworks available, as well as Microsoft's own Prism framework (which is arguably more useful if you have a larger, modular application).
WPF 可能更适合 MVVM(模型-视图-视图模型)。我建议您阅读这篇 MSDN 有关 MVVM 的文章,也许,遵循他们的建议。我在 Bryant Likes 博客。
WPF is probably more well suited to MVVM (Model-View-ViewModel). I'd recommend reading this MSDN article on MVVM and, perhaps, following their advice. There's also a nice collection of links I found on the Bryant Likes blog.