开发者

MVVM: Communication

开发者 https://www.devze.com 2023-03-28 02:46 出处:网络
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

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 开发者_Go百科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?


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, and NavigateBackwardCommand 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 the CustomerID property changes in the data context object, the page view models will need to refresh properties that appear in their respective views.

0

精彩评论

暂无评论...
验证码 换一张
取 消