简体   繁体   中英

MVVM Light - Child Views and Dialogs

I'm experimenting with MVVMLight and Windows Phone 7, and so far find it relatively easy. The one thing I can't get my head around is spawning new child windows/views. For example: if I want to create/navigate to a new view to allow a user to edit an item, then refresh the list of items from the database when they return, should I add some sort of handler for every activation of the view, or can I navigate to the edit view, then trigger a callback when the view is closed (NavigationService.GoBack is called).

What I use for this is the Messaging framework. Have the MainViewModel subscribe to a message that should cause it to refresh, then issue that message from the child page. The MainViewModel, still in memory, will hear that message and be able to respond. I have a sample of this on my blog at http://chriskoenig.net/2010/07/05/mvvm-light-messaging/ , but note that you can also create your own custom messages (I personally do this all the time) and then just raise them manually:

// In MainViewModel
Messenger.Default.Register<ChildProcessCompleteMessage>(this, () => RefreshData());

// In ChildViewModel
Messenger.Default.Send<ChildProcessCompleteMessage>(new ChildProcessCompleteMessage());

This is a pattern I use quite a bit in my apps to allow communication between the view models. I'm not sure if this fully answers your question, so let me know if you need more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM