- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想使用反射订阅 EventAggregator 事件,因为我试图在运行时动态连接 Prism 模块之间的事件订阅。 (我使用的是 Silverlight 5、Prism 和 MEF)。
我想要实现的是调用_eventAggregator.GetEvent<MyType>().Subscribe(MyAction)
在我的一个模块中,但我一直在调用 _eventAggregator.GetEvent<MyType>()
.我怎样才能从那里继续调用 Subscribe(MyAction)
?
假设我的事件类是 public class TestEvent : CompositePresentationEvent<string> { }
.我在编译时不知道这一点,但我知道运行时的类型。
这是我目前得到的:
Type myType = assembly.GetType(typeName); //get the type from string
MethodInfo method = typeof(IEventAggregator).GetMethod("GetEvent");
MethodInfo generic = method.MakeGenericMethod(myType);//get the EventAggregator.GetEvent<myType>() method
generic.Invoke(_eventAggregator, null);//invoke _eventAggregator.GetEvent<myType>();
我非常感谢指向正确方向的指针。
最佳答案
您可以执行此操作而不必担心使用动态调用的事件的“类型”。
Type eventType = assembly.GetType(typeName);
MethodInfo method = typeof(IEventAggregator).GetMethod("GetEvent");
MethodInfo generic = method.MakeGenericMethod(eventType);
dynamic subscribeEvent = generic.Invoke(this.eventAggregator, null);
if(subscribeEvent != null)
{
subscribeEvent.Subscribe(new Action<object>(GenericEventHandler));
}
//.... Somewhere else in the class
private void GenericEventHandler(object t)
{
}
现在您真的不需要知道“事件类型”是什么。
关于c# - 使用反射订阅 Prism EventAggregator 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13035320/
我有以下设置: Silverlight 应用程序拆分 xaps/模块 我使用 MEF 作为 DI 框架来连接 我的应用程序的各个部分。 我有 2 个区域: 一个(左边的)填充有 ListView (例
Micro 2.x 用户, 我在模拟(使用最小起订量)和验证对 EventAggregator.PublishOnUIThread 的调用时遇到问题,因为它是一种扩展方法! 查看源代码让我想出了另一个
这是线程安全的吗? Prism 中的 EventAggregator 是一个非常简单的类,只有一个方法。当我注意到空检查和创建新类型以添加到私有(private) _events 集合中时没有锁定
多亏了这个SO answer我已经成功地测试了 PRISM EventAggregator 的发布事件(用 FakeItEasy 伪装) [TestCase] public void test_tha
我试图让我的 IEventAggregator 允许我在一个模块中发布和事件并在另一个模块中捕获它。我在一个模块/项目中尝试了下面的代码,效果很好。只有当我有一个模块/项目发布事件而另一个订阅它时,它
我通过 Prism 指导工作,并认为我掌握了他们的大多数通信工具。 命令非常简单,因此很明显,DelegateCommand 将仅用于将 View 与其 Model 连接起来。 当涉及到跨模块通信时,
寻找一个简单的 helloWorld EventAggregator 示例。我试图理解这一点,并且在遵循 RI 示例时遇到了一些困难。 谢谢 N 最佳答案 Prism 4.0 包括 EventAggr
我有一个与这篇文章类似的问题 Prism CompositePresentationEvent fires twice 我的问题是 Publish 只触发一次,但 Subscribe 中的代码执行了两
我正在尝试在模块级别而不是从类内部访问 Aurelia EventAggregator 服务。它在一个类中工作正常,我在其中 @inject 事件聚合器,但在外部不行。 import {inject}
我按照教程http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/contact-manager-tutorial/1进行操作
我在 Prism 中使用 EventAggregator,我希望事件持久化,这意味着当客户端订阅某个事件时,聚合器将之前错过的事件发布给客户端。有没有办法支持这个? 我知道这听起来更像是消息队列之类的
我一直在寻找 Composite Application Library ,这很棒,但我无法决定何时使用 EventAggregator...或者更确切地说 - 何时不使用它。 再看看 StockTr
我正在构建一个 Backbone 应用程序,使用 require.js 进行模块化加载,并使用 Marionette 来帮助我的应用程序结构和功能。我已经为事件聚合器设置了一个 require 模块,
我正在学习 Prism 。几个小时我就遇到了一个问题,订阅事件时,订阅方法没有被调用。我正在使用 Prism 和 Autofac . 在下面的简化示例中,主视图型号 Publish("dupa");事
我正在使用 Prism 的 EventAggregator 在模块的 ViewModel 之间进行松散耦合的通信。我在 ViewModelA 中有几个属性(例如 FirstName、LastName)
在我的复合 WPF 应用程序中,我有一个事件,当用户双击控件时发布。模块订阅事件并在必要时执行操作。 此事件似乎随机停止工作。有时当我运行应用程序时,我可以毫无问题地触发事件,有时我只能在模块停止接收
我开始使用 Prism 和 MVVM 开发一个 WPF 项目,我正在尝试使用 eventAggregator,但是当执行下面的行时会引发异常: IServiceLocator ob = Service
我想使用反射订阅 EventAggregator 事件,因为我试图在运行时动态连接 Prism 模块之间的事件订阅。 (我使用的是 Silverlight 5、Prism 和 MEF)。 我想要实现的
我了解 EventAggregator 在 Caliburn Micro 中是如何工作的,但我不明白这一点: 我们以此为例: View 模型 A private IEventAggregator _e
为了简单的模块间通信,有经典的 .NET 事件,但现在太多了,并且存在通过模块相互调用的事件链。 比如 Event_A 触发 Event_B 触发 Event_C。 EventAggregator 对
我是一名优秀的程序员,十分优秀!