事件聚合器的使用过程
1、添加一个UpdateLoadingEvent事件类继承于Prism.Events.PubSubEvent
public class UpdateLoadingEvent : Prism.Events.PubSubEvent
{
}
2、需要使用该事件的页面ViewModel构造函数中订阅该事件
public abstract class ViewModelBase : BindableBase
{
/// <summary>
/// 事件聚合器
/// </summary>
public IEventAggregator Event;
public ViewModelBase(IContainerExtension container)
{
this.Event = container.Resolve<IEventAggregator>();
Event.GetEvent<UpdateLoadingEvent>().Subscribe(CloseLoadingWin, ThreadOption.UIThread);
}
/// <summary>
///
/// </summary>
public void CloseLoadingWin()
{
}
}
3、在需要执行该事件的地方发布
public class ReaderViewModel : BindableBase
{
/// <summary>
/// 事件聚合器
/// </summary>
public IEventAggregator Event;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="container"></param>
public ReaderViewModel(IContainerExtension container)
{
this.Event = container.Resolve<IEventAggregator>();
}
/// <summary>
/// 关闭Loading界面
/// </summary>
public void CloseLoadingWin()
{
Event.GetEvent<UpdateLoadingEvent>().Publish();
}
}
ThreadOption类型
1、ThreadOption.PublisherThread:与事件发布者在同一线程调用执行
2、ThreadOption.UIThread:在UI线程调用执行,如果需要更新UI控件时必须使用该类型
3、ThreadOption.BackgroundThread:在后台线程异步调用执行



评论一下吧
取消回复 评论守则