简体   繁体   English

Silverlight和多个视图模型

[英]Silverlight and multiple viewmodels

I'm writing a Silverlight app using the MVVM pattern. 我正在使用MVVM模式编写Silverlight应用程序。 I have a main view (UserList.xaml) and corresponding vm (UserListViewModel.cs). 我有一个主视图(UserList.xaml)和相应的vm(UserListViewModel.cs)。 This is used to list a collection of users. 这用于列出用户集合。 I also have a UserControl (User.xaml - invoked as a modal dialog) that is used to add details for a new user. 我还有一个UserControl(User.xaml-作为模式对话框调用),用于为新用户添加详细信息。 This also has a viewmodel of it's own (UserViewModel.cs). 它也有自己的视图模型(UserViewModel.cs)。

My questions is how do you pass information (a user object) from the child viewmodel back up to the parent once the child view is dismissed? 我的问题是,一旦子视图被关闭,如何将信息(用户对象)从子视图模型传递回父视图? I need to update the collection in the parent viewmodel when I do a save on the child viewmodel. 在子视图模型上进行保存时,我需要在父视图模型中更新集合。 Should be simple enough but the parent viewmodel has no idea when the child is dismissed. 应该足够简单,但是父级视图模型不知道何时解散子级。 Even if the UserViewModel.User property is changed and it's PropertyChanged event is raised, UserListViewModel is not aware of it. 即使UserViewModel.User属性发生更改,并且引发了PropertyChanged事件,UserListViewModel也不知道。

Any help here is greatly appreciated. 非常感谢您的任何帮助。 I've read everything about mvvm in Silverlight that I can find but there are still some gaps. 我已经在Silverlight中阅读了有关mvvm的所有内容,但仍然存在一些差距。 I hope I've outlined my issue clearly. 我希望我已经清楚地概述了我的问题。

There are many ways of doing this. 有很多方法可以做到这一点。 You could pass an instance of the parent VM to the child VM in the constructor. 您可以将父VM的实例传递给构造函数中的子VM。 You could have the parent VM subscribe to an event that the child VM exposes when your code has run. 您可以让父VM订阅运行代码时子VM公开的事件。

Hope this helps. 希望这可以帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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