[英]Bubble events in viewmodels using Caliburn.Micro
我有一个带有搜索字段和一些结果的屏幕视图模型,该视图模型由多个较小的视图模型组成:用于搜索字段的控件,以及每个结果的控件实例。
我的“容器”视图模型(带有搜索和结果)如下所示:
[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);
}
}
和“快速搜索”(带有搜索字段)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));
}
}
该视图将按预期显示,并且搜索按钮触发SearchReference
方法。
但是该事件似乎没有冒泡到ShippingViewModel
,从未触发过Handle
。
谢谢!
看来您从未订阅ShippingViewModel
的事件。 尝试像这样修改构造函数:
[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.