我应该如何从 P&P 为 EventAggregator 创建事件,以便 UI 线程上的订阅者可以收听它们?
我正在尝试在后台任务运行时更新主窗体中的进度条。
我正在使用最新的 Patterns & 中的 EventAggregator。实践发布路线我的应用程序范围内的事件。
我正在从一个监听BackgroundWorker事件的类中触发一个事件,然后触发一个事件,如下所示:
- Process on bw fires the BW method 报告进展情况。
- BW 火了 报告事件。
- 他们被选中 通过 SomeCommand 类方法 之前已在 BW 上设置 推出。
- 我发布事件来自 EventAggregator
public void ProgressChanged(对象发送者,ProgressChangedEventArgs ea) { KnownProgressStatusChangedEvent evt = KernelKeeper.Kernel.Get().GetEvent(); evt.Publish(ea); 我
的 MainPresenter 已订阅这些事件,如下所示:
KnownProgressStatusChangedEvent progressChanged = EventAggregator.GetEvent<KnownProgressStatusChangedEvent>();
progressChanged.Subscribe(KnownProgressChanged,ThreadOption.UIThread);
如果我不设置 ThreadOption.UIThread,我会在 Program.cs 中收到 TargetInvokationException,且没有堆栈跟踪。 这样我就不会出现异常,并且可以进入 EventAggregator。
当它要调用 KnownProgressChanged 方法时,它会尝试调用该方法并检查 Application.Current != null
。它为空并且没有任何内容被触发。
我做错了什么?请指教。
I am trying to update a progress bar in my main form while a background task is running.
I am using the EventAggregator from the latest Patterns & Practices release route my application wide events.
I am firing an event from a class listens to BackgroundWorker events and than fires an event as such:
- Process on bw fires the BW method
to report progress. - BW fires it's
reporting events. - They get picked
up by the SomeCommand class methods
were set on the BW before it was
launched. - I publish Events from
the EventAggregator
public void ProgressChanged (object sender, ProgressChangedEventArgs ea)
{
KnownProgressStatusChangedEvent evt = KernelKeeper.Kernel.Get().GetEvent();
evt.Publish(ea);
}
My MainPresenter has subscribed to those events as such:
KnownProgressStatusChangedEvent progressChanged = EventAggregator.GetEvent<KnownProgressStatusChangedEvent>();
progressChanged.Subscribe(KnownProgressChanged,ThreadOption.UIThread);
If I don't set the ThreadOption.UIThread I get TargetInvokationException in the Program.cs with no stack trace.
This way I get no exception and I can step in the EventAggregator.
When it is about to call the KnownProgressChanged method it tries to invoke it and checks for Application.Current != null
. It is null and nothing is fired.
What am I doing wrong ? Please advise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须指定 ThreadOption.UIThread 因为您正在处理进度条,必须从 ui 线程调用处理程序才能绘制新的进度状态。
但是,如果您使用 WPF,则必须在没有 ThreadOption.UIThread 和 dispatch 自己调用,您可以查看 CompositeWpfEvent。
请参阅事件聚合器 - 在用户界面线程上订阅
You have to specify ThreadOption.UIThread because you are dealing with a progress bar, the handler must be called from the ui thread to be able to draw the new progress state.
Howerver if you are working with WPF you have to handle it without ThreadOption.UIThread and dispatch the call yourself, you may take a look at the CompositeWpfEvent.
See Event Aggregator - Subscribing on the User Interface Thread
TargetIncationException 是一个转移注意力的问题,它是我在代码中的其他地方抛出未实现的异常而引起的。
我正在使用 WinForms 并使用带有 ThreadOption.PublisherThread 选项的订阅,效果很好。
TargetInvocationException was a red herring, which I caused by throwing a not implemented exception elsewhere in my code.
I am using WinForms and used the Subscribe with the ThreadOption.PublisherThread option and that works fine.