Prism - EventAggregator.GetEvent<>.Subscribe() - Using Generics & Contraints

泪湿孤枕 提交于 2019-12-02 01:49:57

MyEvent<IRequest> and MyEvent<Profile> are not the same, so the EventAggregator doesn't see this as the same event. To allow for covariant types, you can fix it with the out modifier in C# 4. Something like this should work:

public interface IMyClass<out T> where T : IRequest { }

public class MyClass<T> : IMyClass<T> where T : IRequest { }

public class MyEvent<TValue> : PubSubEvent<IMyClass<TValue>> where TValue : IRequest { }

Then your event aggregator would look something like:

 _eventAggregator.GetEvent<MyEvent<IRequest>>().Subscribe(Received);

 private void Received(IMyClass<IRequest> obj)
 {

 }

_eventAggregator.GetEvent<MyEvent<IRequest>>().Publish(new MyClass<Profile>());

Does this help?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!