[英]Bubble events in viewmodels using Caliburn.Micro
I have a screen viewmodel with a search field and some results, composed of multiple, smaller viewmodels: a control for the search field, and a control instance for each result. 我有一个带有搜索字段和一些结果的屏幕视图模型,该视图模型由多个较小的视图模型组成:用于搜索字段的控件,以及每个结果的控件实例。
My "container" viewmodel (with the search and results) looks like this: 我的“容器”视图模型(带有搜索和结果)如下所示:
[Export(typeof(ShippingViewModel))]
public class ShippingViewModel : Screen, IHandle<SearchReferenceEvent>
{
private readonly IEventAggregator events;
[ImportingConstructor]
public ShippingViewModel(IEventAggregator events)
{
this.events = events;
this.Search = new QuickSearchViewModel(this.events);
}
public QuickSearchViewModel Search { get; set; }
public void Handle(SearchReferenceEvent message)
{
System.Windows.MessageBox.Show(message.Reference);
}
}
And the "quick search" (with the search fields) viewmodel: 和“快速搜索”(带有搜索字段)viewmodel:
[Export(typeof(QuickSearchViewModel))]
public class QuickSearchViewModel : PropertyChangedBase
{
private readonly IEventAggregator events;
private string currentSearch;
[ImportingConstructor]
public QuickSearchViewModel(IEventAggregator events)
{
this.events = events;
}
public string CurrentSearch // bound to the search field
{
get
{
return this.currentSearch;
}
set
{
this.currentSearch = value;
this.NotifyOfPropertyChange(() => this.CurrentSearch);
}
}
public void SearchReference(string reference) // bound to the search button
{
this.events.Publish(new SearchReferenceEvent(reference));
}
}
The view is displayed as expected, and the search button triggers the SearchReference
method. 该视图将按预期显示,并且搜索按钮触发SearchReference
方法。
But the event doesn't seem to be bubbled up to the ShippingViewModel
, the Handle
is never triggered. 但是该事件似乎没有冒泡到ShippingViewModel
,从未触发过Handle
。
Thanks! 谢谢!
Looks like you're never subscribing to the events in your ShippingViewModel
. 看来您从未订阅ShippingViewModel
的事件。 Try modifying your constructor like so: 尝试像这样修改构造函数:
[ImportingConstructor]
public ShippingViewModel(IEventAggregator events)
{
this.events = events;
this.events.Subscribe(this); // <= register to receive events
this.Search = new QuickSearchViewModel(this.events);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.