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

后端 未结 1 1114
借酒劲吻你
借酒劲吻你 2021-01-21 13:45

I am having an issue subscribing to events with the event Aggregator that comes as part of the prism framework.

If I use something such as

eventAggregato         


        
相关标签:
1条回答
  • 2021-01-21 14:04

    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?

    0 讨论(0)
提交回复
热议问题