VB WPF MVC(模型+视图+?)

发布于 2024-09-08 02:21:41 字数 316 浏览 1 评论 0原文

我有一个旧的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗弃M 2024-09-15 02:21:41

下面是关于 MVC 模式是什么以及如何将其应用到 WPF 应用程序的“简要”描述。

(我可能有一些细节略有偏差,因为我主要是在 Silverlight 中进行黑客攻击,但概念足够相似。)

基本上,这个想法是分离关注点并定义应用程序不同部分之间的接口,目的是保持代码结构化且可维护。

您示例中的 Model 与您描述的轮辋几乎完全相同 - 一个 WheelRim 类,具有以合适的数据类型定义的各种属性。我会将模型放在一个单独的程序集中,以将其与其他代码分开,但您可以将模型类保留在“模型”文件夹中。该模型在数据库中还有一个“双胞胎”,模型类几乎一对一映射到表。

(您可能想看看 Linq2SQL 或实体框架,如果定义了数据库,您几乎可以免费获得模型以及合适的数据库访问代码。)

View 将是实际的 WPF xaml文件 - 定义网格或画布或您拥有的东西。在 WheelRimView 上,会有用于显示或访问不同属性的标签和文本框,可能还有产品图像等。视图后面的代码将包含所有相关的事件处理程序(start、button_click 等),用于从字段获取数据并将其传递到控制器。

控制器可以是您用来操作数据的任何“处理程序代码”。我们在这里讨论基本的 CRUD 操作,以及验证等。此外,控制器层将负责将数据编译为可以无缝进入视图的格式。因此,WheelRimController 将具有“GetWheelRimList”、“GetWheelRim”、“AddWheelRim”、“ModifyWheelRim”和“DeleteWheelRim”等方法。这些方法将值作为参数并相应地操作模型对象。我

建议保持 xaml 文件的代码隐藏不受任何“控制器”式代码的影响,例如验证、聚合等 - 后面的代码基本上应该只从文本框、列表框等中获取值并将它们发送到“按原样”到控制器方法进行处理。此外,在获取用于演示的数据时,您应该将任何数据格式化代码保持在最低限度(即,不在视图中进行过滤或转换)。

“用户打开轮辋并编辑直径”的典型用例将在代码中执行:

  1. 用户单击列表页面上的“编辑”。 WheelRimView 页面加载。
  2. WheelRimView.Load() 方法(或相应方法)调用 WheelRimController.GetWheelRim(wheelRimId)。
  3. WheelRimController.GetWheelRim(wheelRimId) 从数据库表中获取相应数据并填充 WheelRim 对象的属性,该对象返回到 WheelRimView。
  4. WheelRimView 将属性值插入到标签和文本框中。
  5. 用户更改直径值并单击“保存”按钮。WheelRimView.Save
  6. () 方法调用 WheelRimController.ModifyWheelRimDiameter(wheelRimId,diameter) 方法。WheelRimController.ModifyWheelRimDiameter
  7. (wheelRimId,diameter) 方法解析直径(如果是字符串)并将修改后的值应用于模型对象并将其保存到数据库中。
  8. WheelRimController.ModifyWheelRimDiameter(wheelRimId,直径)将状态代码返回到WheelRimView(例如,指示任何验证错误的预定义数字。 )来报告保存成功。WheelRimView
  9. 会向用户显示一条结果消息(希望是“已保存”)

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:

  1. User clicks "Edit" on a list page. The WheelRimView page loads.
  2. The WheelRimView.Load() method (or corresponding) calls WheelRimController.GetWheelRim(wheelRimId).
  3. WheelRimController.GetWheelRim(wheelRimId) gets the corresonding data from a database table and populates the properties of a WheelRim object, which is returned to the WheelRimView.
  4. The WheelRimView inserts the property values into the labels and textboxes.
  5. The user changes the diameter value and clicks the "Save button.
  6. The WheelRimView.Save() method calls the WheelRimController.ModifyWheelRimDiameter(wheelRimId, diameter) method.
  7. The WheelRimController.ModifyWheelRimDiameter(wheelRimId, diameter) method parses the diameter (if it is a string) and loads the model object. It applies the modified value to the model object and saves it into the database.
  8. The WheelRimController.ModifyWheelRimDiameter(wheelRimId, diameter) returns a status code to the WheelRimView (for instance a predefined numeric stating any validation errors) to report the success of the save.
  9. The WheelRimView displays a result message (hopfully "saved") to the user.

I hope that clears a few bits up.

沉鱼一梦 2024-09-15 02:21:41

由于提供了丰富的绑定支持,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).

七七 2024-09-15 02:21:41

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文