WPF、MVVM 和 Prism 模块化

发布于 2024-09-24 09:45:37 字数 594 浏览 3 评论 0 原文

我仍在学习使用 MVVM 和 Prism,并有一些一般性问题:

  • 我的视图中有一个网格。可以说我 有一个按钮,当我点击时我 希望它自动调整网格大小 列。该代码会去哪里? 调整网格列的大小是一个视图问题 并且视图模型不应该知道 关于它。所以在这种情况下我会 添加按钮点击处理程序 视图的代码在后面吗?我有 与网格编辑相同的问题和 验证。视图模型可以看到 当使用双向编辑值时 具有约束力,但如果它决定 值无效,如何通知 取消编辑的网格?

  • 假设我的视图有许多用户 控件和每个用户的控件需求 绑定到来自不同对象的数据。该视图的视图模型是否只是一个巨大的类,其中包含视图的所有不同组件所需的所有数据?

  • 关于Prism和模块化设计,我 我想弄清楚什么是 “模块”是。我的理解是 模块是独立的, 意思是如果我拿起我的模块并且 将其放入另一个应用程序中,它应该 工作。所以如果我有课 进行一些服务调用(让 说对服务器的 SOAP 调用得到一些 信息)并填充网格,我的模块 需要包括 MVVM 组件和我的服务层, 正确的?如果我有多个模块 使用相同的服务层,确实 每个都需要包含服务层类的副本 为了被认为是一个完整的 模块?

感谢您的任何建议/信息。

I am still learning to use MVVM and Prism and have some general questions:

  • I have a grid in my view. Lets say I
    have a button that when I click I
    want it to auto size the grid
    columns. Where would that code go?
    Resizing grid columns is a view thing
    and the view model shouldn't know
    about it. So in this case would I be
    adding the button click handler in
    the view's code behind? I have the
    same question with grid editing and
    validation. The view model can see
    when a value is edited with two-way
    binding but if it decides that the
    value is invalid, how can it notify
    the grid to cancel the edit?

  • Lets say my view has a number of user
    controls and each user control needs
    to bind to data from a different object. Would my view model for this view just be a huge class with all of the data I would need for all of the different components of the view?

  • Regarding Prism and modular design, I
    am trying to figure out what a
    "module" is. My understanding is
    that a module is self contained,
    meaning if I pick up my module and
    drop it in another app, it should
    work. So if I have a class
    that makes some service calls (lets
    say SOAP calls to a server get some
    info) and populates a grid, my module
    would need to include both the MVVM
    components and my service layer,
    right? If I have multiple modules
    that use the same service layer, does
    each one need to include a copy of the service layer classes
    in order to be considered a complete
    module?

Thanks for any advice/info.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

╭ゆ眷念 2024-10-01 09:45:37

我将尝试分别讨论这些项目。

  1. 正如您所说,调整网格列的大小更多的是一个视图问题,因此将其保留为 UI 逻辑的一部分可能是最好的事情。为了使代码尽可能干净,您可以使用不同的东西,例如 行为以及在某些情况下路由命令以避免代码隐藏充满代码。至于验证逻辑,您可能应该将其放置在 VM/M 中并使用 WPF 的验证功能(例如 IDataErrorInfo 等)。
  2. 您可以为所有用户控件使用单一视图模型,或者采用分层视图模型方法。 最新的 Prism drop 在 MVVM RI 中显示了这种情况。
  3. 您对模块的理解是准确的。需要考虑的一件事是,如果整个应用程序有一个通用服务,通常的方法是将其接口放在基础设施项目中,并在需要时引用它(具体实现通常通过 DI 或 Mef 导入获得)。具体实现所属的模块可以根据需要选择在容器中注册服务/暴露导出。 此线程提供有关模块、它们是什么以及如何使用的更多信息应该使用它们。

如果您刚开始使用 Prism 并有任何其他问题,您可能会发现 codeplex 论坛很有用,例如也许 Prism SE 团队已经回答了您的问题。

我希望这个答案有帮助。

谢谢,
达米安

I'll try to go over these items separately.

  1. As you said, resizing the grid columns is more of a view thing, so keeping that as part of the UI logic would probably be the best thing. With the intention of keeping the code behind as clean as possible, you can use different things such as behaviors and in some scenarios routed commands to avoid having your code-behind filled with code. As for the validation logic, you should probably place that in the VM/M and use WPF's validation capabilities (such as IDataErrorInfo and others).
  2. You can have a single view model for all user controls or take a hierarchichal view model approach. The latest Prism drop, shows this scenario in the MVVM RI.
  3. Your understanding of modules is accurate. One thing to consider, is that if you have a common service for the entire application, the usual approach is placing its interface in the Infrastructure project and reference it when necessarry (the concrete implementation is usually obtained via DI or Mef imports). The module that the concrete implementation belongs to can choose to register the service in the container/expose the export as required. This thread has more information about Modules, what they are, and how they should be used.

If you are getting started with Prism and have any other questions, you might find the codeplex forum useful, as perhaps your question has been answered there by the Prism SE team.

I hope this answer helps.

Thanks,
Damian

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