C#,WPF 在代码隐藏中订阅外部事件
我正在尝试使用 WPF 中的代码隐藏来订阅外部类中的事件,但显然失败了。
我有一个外部库,它使用 ActiveMQ.NMS API 与代理进行通信并异步侦听主题。要执行第二个操作,我们需要注册一个名为 consumer.Listener 的事件。一切都很完美,直到创建一个引用该库的 WPF 项目并尝试从代码隐藏订阅事件(在代码隐藏中,我调用了负责与代理通信的对象的实例,创建了处理程序并注册了)。
据我现在了解,我的问题背后的原因是线程亲和力,但是我仍然很想知道如何从代码隐藏中做我想做的事情,尽管大多数开发人员会立即鄙视我,因为我什至想到了这样的事情。附带说明一下:有点有趣的是,调试器在没有给出任何理由的情况下,如果想要这样做,就跳过方法中的一些行。
另外,在 WPF 中处理外部事件的推荐模式是什么?
感谢您的反馈:)
I'm trying to subscribe for an event in an external class using code-behind in WPF and I obviously fail.
I have an external library which uses ActiveMQ.NMS API to communicate with a broker and asynchronously listen on a topic. To do the second we need to register to an event called consumer.Listener. Everything worked perfect until creating a WPF project referencing the library and trying to subscribe to the event from code-behind (in code behind I called instance of an object responsible for communicating with the broker, created the handler and registered it).
As I understand now the reason behind my problem is Thread Affinity, however I would still love to know how to do what I was trying to do from code-behind even though most developers will immediately despise me for even thinking about such a thing. As a side note: it's a bit funny that debugger without giving any reason just skips some lines in methods if it feels like doing that.
Also, what is the recommended pattern for dealing with external events in WPF?
Thank you for your feedback :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设当你说“外部事件”时,你只是指由某种对象暴露的公共事件。但是,在引用使用 ActiveMQ.NMS API 的库时,您使用术语“外部”...所以我假设在这种情况下,您的意思是它是代码库之外的库,是您导入、引用等的库。请更正/编辑您的如果我误解了,请发帖。
假设“consumer”是您在项目中引用的库中的一个对象,一旦实例化“consumer”,您应该有一段如下所示的代码:
输入“new”后,智能感知应该填写其余部分如果您按两次 Tab 键,则会自动为您提供帮助。
我不知道为什么你认为大多数开发人员会因为你的想法而鄙视你......这通常是一个很好的做法。
I'm going to assume that when you say "external events" you simply mean public events, exposed by an object of some kind. However you use the term external when referring to the library which uses ActiveMQ.NMS API... so I assume in that context you mean that it's a library outside of your code base, something you import, reference etc. Please correct / edit your post if I'm misunderstanding.
Assuming "consumer" is an object inside the library you reference with your project, once you instantiate "consumer", you should have a piece of code that looks something like:
After you finish typing in "new" the intellisense should fill out the rest for you automatically if you hit the tab key twice.
I don't know why you think most developers would despise you for thinking about that... it's usually a good practice.