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

后端 未结 1 1137
借酒劲吻你
借酒劲吻你 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 and MyEvent 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 where T : IRequest { }
    
    public class MyClass : IMyClass where T : IRequest { }
    
    public class MyEvent : PubSubEvent> where TValue : IRequest { }
    

    Then your event aggregator would look something like:

     _eventAggregator.GetEvent>().Subscribe(Received);
    
     private void Received(IMyClass obj)
     {
    
     }
    
    _eventAggregator.GetEvent>().Publish(new MyClass());
    

    Does this help?

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