WPF - MVVM 屏幕管理
想象一下您有一个复杂的数据对象。它足够复杂,以至于要编辑对象的各种属性,用户最好拥有多个屏幕。它本质上是一个配置项目的购物车。
因此,一个屏幕就可以让您添加项目。 另一种方法允许您对这些项目进行修改,即预先确定的更改,这些更改会产生相关成本。 第三个屏幕允许您配置项目的全局设置。
我相信您可以猜到,每个屏幕都在完全相同的购物车上运行,只是改变了内部物品的不同属性和关系。
因此,我们将尝试使用 MVVM 编写应用程序,在讨论各种屏幕(以及它们之间的导航)时,我们得出以下问题:
人们在使用 MVVM 时通常如何管理应用程序状态?用户用来切换屏幕的导航栏将存在于屏幕之外,但是当用户单击它时,人们使用哪些常见方式来隐藏一个并显示另一个?
更一般地说,人们如何处理全局应用程序状态?用户一次只能操作一辆购物车,一次只能有一个用户登录,一次只能显示一个屏幕。最好创建一个存储这些重要属性的单例,并且 ViewModel 可以保留它们的副本并通过事件聚合器订阅更改吗?
正如你所知,我什至不知道从哪里开始解决这个问题,所以任何建议都受到欢迎和赞赏。
Imagine you have a complex data object. It was complex enough that to edit the various properties of the object, it would be best for the user to have multiple screens. It's essentially a shopping cart for configured items.
So one screen would allow you to add items.
Another would allow you add modifications to those items, predetermined changes that have a cost associated.
A third screen would allow you to configure global settings for your items.
As I'm sure you can guess, each screen is operating on the exact same cart, just altering different properties and relationships of the items inside.
So, we're going to try to write the application using MVVM, and while discussing the various screens (as well as navigation between them) we arrived at the following question:
How do people generally manage application state when using MVVM? The navigation bar that the users will use to change screens will exist outside of the screen, but when a user clicks it, what common ways have people been using to hide one and show another?
More generally, how are people handling global application state? The user can only operate on one cart at a time, there can only be one user logged in at a time, only one screen can be shown at a time. Would it be best to create a singleton that stored these important properties and the ViewModels could keep a copy of them and subscribe to changes via an event aggregator?
As you can tell, I barely even know where to start with this problem, so any advice at all is welcomed and appeciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用 ViewModel 来跟踪应用程序状态。
一个 ViewModel 控制整个应用程序,并处理用户当前所在的页面。应用程序本身绑定到主 ViewModel,并且应用程序屏幕空间的大部分是绑定到 ViewModel.CurrentPage 的 ContentControl。然后,DataTemplates 用于确定为用户当前所在的任何页面显示哪个视图。
过去,我对某些对象(例如当前用户)使用了全局单例,并且 ViewModel 在需要时使用对此的引用。因此,如果我想在页面上显示 UserName,我会在 ViewModel 中有一个名为 UserName 的属性,它返回 Global.Instance.CurrentUser.UserName
I would use ViewModels to track the application state.
One ViewModel controls the entire application, and it handles what page the user is currently on. The application itself is bound to the main ViewModel, and the majority of the application screen space is a ContentControl that is bound to ViewModel.CurrentPage. DataTemplates are then used to determine which View to display for whatever page the user is currently on
In the past I've used a global singleton for some objects (such as current user) and the ViewModels use a reference to this if needed. So if I wanted to display the UserName on a page, I'd have a property in the ViewModel called UserName and it returns
Global.Instance.CurrentUser.UserName
对于您的情况类型,我会研究 PRISM。 PRISM 是用于以松散耦合的 MVVM 方式开发 WPF 应用程序的模式集合。
具体来说,对于多个屏幕和管理应用程序状态的示例,使用“控制器”加载将 ViewModel(购物车)的各种表示形式的视图放入单独的“区域”可能是一个好的开始。 MSDN 上似乎有一篇关于PRISM 入门的精彩文章,包括组合用户界面(区域)。
For your type of situation I would look into PRISM. PRISM is a collection of patterns for developing WPF applications in a loosely coupled MVVM manner.
Specifically, for your example of the multiple screens and managing application state, using a "Controller" to load the views for the various representations of your ViewModel (the cart) into separate "Regions" would probably be a good start. There looks to be a great article on MSDN about getting started with PRISM, including composing user interfaces (Regions).