搭载 iNotifyPropertyChanged 系统的视图模型之间的通信
我正在使用 MVVM,我想在视图模型之间进行通信。我有一个用户控件,其中包含另一个用户控件,并且我希望父用户控件在子控件中的属性更改时运行一些代码。我已经看到了几种在视图模型之间进行通信的方法,例如使用 MVVM Light Messenger 或 PRISM Event Aggregator,但我希望有某种方法可以简单地通过订阅通过引发的 PropertyChanged
事件来实现此目的INotifyPropertyChanged
实现。
马特·汉密尔顿 (Matt Hamilton) 在 这篇文章但我在实现它时遇到了麻烦,因为它需要一个 DependencyObject,并且我的 ViewModel 是 POCO 而不是 DO
有没有某种方法可以使用INotifyPropertyChanged
系统,因为我不想实现消息传递系统。如果不是,消息系统是最好的吗?我还看到一个示例,其中有人只是使用视图后面的代码来帮助传递属性,但是我不想破坏 MVVM 模式,因为我想在稍后阶段进行一些测试。
I am using MVVM and I would like to communicate between viewmodels. I have a user control that contains another user control inside it, and I would like the parent usercontrol to run some code when a property in the child is changed. I have seen several ways to communicate between viewmodels, such as using MVVM Light Messenger, or PRISM Event Aggregator, but I was hoping there was some way to accomplish this simply by somehow subscribing to the PropertyChanged
event raised through the INotifyPropertyChanged
implementation.
There is an answer by Matt Hamilton in this post but I have trouble implementing it because it needs a DependencyObject, and my ViewModels are POCOs rather than DOs
Is there some way of using the INotifyPropertyChanged
system, as I would prefer to not have to implement a messaging system. If not is a messaging system the best? I also saw an example where some guy just used the code behind of the view to help pass the property, however I don't want to break the MVVM pattern as I wanna do some testing at a later stage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
肯定有多种方法来处理您的情况。一种肯定是使用 INotifyPropertyChanged 实现来传播您的事件。问题是容器需要直接引用子 ViewModel 才能订阅
PropertyChanged
事件。类ParentVM
{
私有 const string SomePropertyName = "SomeProperty";
您还可以显式定义一个事件并订阅它。
There definately multiple ways to handle your scenario. One is certainly to use the
INotifyPropertyChanged
implementation to propogate your event. The issue is that container would need a direct reference to the child ViewModel to subscribe to thePropertyChanged
event.class ParentVM
{
private const string SomePropertyName = "SomeProperty";
You could also explicitly define an event and subscribe to it.
就我个人而言,我会将父视图模型的引用传递给每个子视图模型以进行直接访问。
我倾向于尽可能避免使用 MVVM Light“Messenger”。 (使用 IoC 容器时它有点没用,但那是另一个故事了)
Personally I would pass a reference of the parent viewmodel to each child viewmodel to have direct access.
I tend to avoid the MVVM Light "Messenger" as much as possible. (it's kind of useless when using IoC containers but that's another story)