使用 WinForms 的 MVVM/表示模型
我目前正在开发一个棕地应用程序,它是用 winforms 编写的, 作为在更高版本中使用 WPF 的准备,我们的团队计划至少使用 MVVM/演示模型,并将其绑定到 winforms...
我已经探索了这个主题,包括这个网站中的帖子(我非常喜欢), 归根结底,wpf 的主要优点是:
- 将控件绑定到 xaml 中的属性。
- 将命令绑定到视图模型中的命令对象。
第一个功能很容易实现(在代码中),或者使用通用控件绑定器,它绑定表单中的所有控件。
第二个功能实现起来有点困难,但是如果您从所有控件继承并添加一个命令属性(由单击等内部事件触发),该属性将绑定到 ViewModel 中的命令实例。
我目前意识到的挑战是:
- 实现一个命令管理器(这将根据需要触发命令的 CanInvoke 方法。winforms
- 仅支持一级数据绑定:数据源、数据成员、wpf 更加灵活。
我是否还缺少其他级别的数据绑定)当尝试实现这种设计模式时,winforms 与 wpf 相比缺乏哪些主要功能?
我确信你们中的许多人会推荐某种 MVP 模式,但 MVVM/Presentation 模型对我来说是正确的选择,因为我想要未来 WPF 支持。
预先感谢 埃里克.
I'm currently working on a brownfield application, it's written with winforms,
as a preparation to use WPF in a later version, out team plans to at least use the
MVVM/Presentation model, and bind it against winforms...
I've explored the subject, including the posts in this site (which i love very much),
when boiled down, the main advantage of wpf are :
- binding controls to properties in xaml.
- binding commands to command objects in the viewmodel.
the first feature is easy to implement (in code), or with a generic control binder, which binds all the controls in the form.
the second feature is a little harder to implement, but if you inherit from all your controls and add a command property (which is triggered by an internal event such as click), which is binded to a command instance in the ViewModel.
The challenges I'm currently aware of are :
- implementing a commandmanager, (which will trigger the CanInvoke method of the commands as necessery.
- winforms only supports one level of databinding : datasource, datamember, wpf is much more flexible.
am i missing any other major features that winforms lacks in comparison with wpf, when attempting to implement this design pattern?
i sure many of you will recommend some sort of MVP pattern, but MVVM/Presentation model is the way to go for me, because I'll want future WPF support.
Thanks in advance,
Erik.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请查看更新控件.NET。它是一个适用于 Winforms、WPF 和 Silverlight 的开源库,可在数据更改时使控件保持最新状态。您现在可以开始将它用于 Winforms,然后转换到 WPF,而无需更改数据模型或视图模型代码。
不幸的是,它并没有解决Winforms命令绑定问题。您的按钮单击事件不会从 Winforms 移植到 WPF。但它确实解决了数据绑定问题。
Please take a look at Update Controls .NET. It is an open-source library for Winforms, WPF, and Silverlight that keeps controls up to date as data changes. You can start using it now for Winforms, and then transition over to WPF without changing your Data Model or View Model code.
Unfortunately, it does not solve the Winforms command binding problem. Your button click events will not port from Winforms to WPF. But it does take care of the data binding problem.
您可能会找到WAF Windows 表单适配器 有趣。它展示了如何在 Windows 窗体应用程序中应用模型-视图-视图模型 (MVVM) 模式。适配器实现为 Windows 窗体中缺少的命令支持提供了解决方案。
You might find the WAF Windows Forms Adapter interesting. It shows how to apply the Model-View-ViewModel (MVVM) Pattern in a Windows Forms application. The Adapter implementation provides a solution for the missing Command support in Windows Forms.