开发者

MVVM Light - Child Views and Dialogs

I'm experimenting with MVVMLight and Windows Ph开发者_运维问答one 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜