MVVM:通信
我正在开发一个大型 MVVM 应用程序。我为此使用 MVVM light 工具包。该应用程序就像一个带有后退和前进按钮的网络浏览器。主视图是用户控件。我将后退和前进按钮放置在主视图用户控件中。主视图又具有用户控件。单击后退和前进按钮时,用户控件会发生变化。主 ViewModel 跟踪当前用户控件并根据按钮单击加载下一个控件。
所有这些用户控件的加载取决于第一步中所做的选择(ID)。比方说,主视图是一个搜索屏幕,我们选择一个客户。接下来的屏幕将是地址、账单、请求等。这些屏幕不共享任何数据。但这些数据是针对同一客户的。
那么,将客户 ID 存储在主视图中是一个好的做法吗?如果我这样做,我应该将 UserControl_Loaded 事件绑定到命令,然后我将在其中请求地址和帐单信息。
或者,我可以将按钮(后退和前进按钮)移动到每个用户控件而不是主视图,传递客户 ID 以及将加载下一个视图的消息。
哪个更好?
I am working on a large MVVM application. I am using the MVVM light toolkit for this. The application is like a web browser with back and forward buttons. The main view is an user control. I laid the back and forward buttons in the main view user control. The main view in turn has user controls. The user controls change when the back and forward buttons are clicked. The main ViewModel keeps track of the current user control and loads the next one depending on the button click.
All these user controls are loaded depending on the selection(ID) made on the first step. Lets say, the main view is a search screen and we select a customer. The next screens would be Address, Billing, Requests, etc. These screens does not share any data. But the data is for the same customer.
So, is it a good practice, to store the customer ID in the main view? If I do this, I should have a UserControl_Loaded event bound to a command, where I would then request for Address and Billing Info.
Or I can move the buttons(back and forward buttons) to each user control instead of the main view, Pass the customer ID with the message which would load the next view.
Which is better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我过去完成此类事情的一种方法是实现一个封装操作数据上下文的类。所有页面都将填充(并更新)此类的属性。主视图模型创建此类的实例和页面视图模型的集合,为每个模型提供数据上下文。它还处理从一个页面到另一个页面的导航,实现
CurrentPage
、NavigateForwardCommand
和NavigateBackwardCommand
属性。如果用户返回到第 1 页并更改客户 ID,则数据上下文将使用适合新客户的信息重新填充。由于所有页面都查看相同的数据上下文对象,因此所有后续页面都将显示正确的信息。
您需要在数据上下文对象中实现属性更改通知,并在页面中处理
PropertyChanged
。当数据上下文对象中的CustomerID
属性发生更改时,页面视图模型将需要刷新出现在各自视图中的属性。A way I've done this sort of thing in the past is to implement a class that encapsulates the data context for the operation. All of the pages will be populated with (and update) properties of this class. The main view model creates an instance of this class and a collection of the page view models, providing each with the data context. It also handles navigation from page to page, implementing
CurrentPage
,NavigateForwardCommand
, andNavigateBackwardCommand
properties.If the user backs up to page 1 and changes the customer ID, the data context is repopulated with the information appropriate to the new customer. Since all of the pages are looking at the same data context object, all of the subsequent pages will display the right information.
You'll need to implement property-change notification in the data context object, and handle
PropertyChanged
in the pages. When theCustomerID
property changes in the data context object, the page view models will need to refresh properties that appear in their respective views.