在 MVVW 中,应该如何将事件从一个 ViewModel 传播到另一个 ViewModel?
我是 MVVW 模式的新手,所以如果我问一个非常基本的问题,你必须原谅我。
我有两个 ViewModel,我们将它们称为 TreeViewViewModel 和 ListViewViewModel。 TreeViewViewModel 绑定到其视图中的 IsSelected 属性。每当 IsSelected 发生变化时,我需要通知 ListViewViewModel 以便它可以更新其视图。
经过一些在线研究后,我发现了 EventAggregator,它看起来可能是一个很好的解决方案。
这是正确的解决方案吗?如果是这样,我应该如何实施?或者,我应该考虑更好的解决方案吗?下面是我认为 EventAggregator 如何集成到发布事件的 ViewModel 中的简化版本。
public class TreeViewViewModel : INotifyPropertyChanged
{
public bool IsSelected
{
get { return _isSelected; }
set
{
if (value == _isSelected)
return;
_isSelected = value;
OnPropertyChanged("IsSelected");
// Is this sane?
_eventAggregator.GetEvent<TreeViewItemSelectedEvent>().Publish(value);
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I'm brand new to the MVVW pattern, so you'll have to forgive me if I'm asking a very basic question.
I have two ViewModels, we'll call them TreeViewViewModel and ListViewViewModel. TreeViewViewModel binds to an IsSelected property in its view. Whenever IsSelected changes, I need to inform ListViewViewModel so that it can update it's view.
After some research online, I've come across the EventAggregator which looks like it might be a good solution.
Is this the right solution? If so, how should I go about implementing this? Or, is there a better solution I should be considering? Below is a simplified version of how I think the EventAggregator might be integrated into the ViewModel publishing the event.
public class TreeViewViewModel : INotifyPropertyChanged
{
public bool IsSelected
{
get { return _isSelected; }
set
{
if (value == _isSelected)
return;
_isSelected = value;
OnPropertyChanged("IsSelected");
// Is this sane?
_eventAggregator.GetEvent<TreeViewItemSelectedEvent>().Publish(value);
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您当然可以使用事件聚合器,但您不需要事件聚合器来完成如此简单的事情。您只需让
ListViewViewModel
监听TreeViewViewModel.PropertyChanged
即可。You can certainly use an event aggregator, but you don't need one for something as simple as this. You can simply have
ListViewViewModel
listen toTreeViewViewModel.PropertyChanged
.EventAggregator 是一个不错的选择,您的代码对我来说看起来是正确的。
其他选项是 SharedService 或只是从一个视图模型直接引用另一个视图模型。
Prism 框架有一个关于这个主题的很好的文档:
http://msdn.microsoft.com/en-我们/library/ff921122(v=PandP.40).aspx
EventAggregator is a good option and your code look correct to me.
The other options would be SharedService or simply having direct reference from one viewmodel to another.
Prism framework has a nice documentation on this topic:
http://msdn.microsoft.com/en-us/library/ff921122(v=PandP.40).aspx
您的另一种选择是中介模式,这是一个示例:
Your other option is the mediator pattern, here is one example: http://marlongrech.wordpress.com/2009/04/16/mediator-v2-for-mvvm-wpf-and-silverlight-applications/