locked
How to implement Event Aggregator (publish/subscribe) in windows phone 8.1 application without using prism.

    Question

  • Hi,

    Currently we have our application already present in Silverlight 8.1. We have implemented our own Event Aggregator for publishing an event. To subscribe/unsubscribe the event we have used ObserveOn and Subscribe methods from Microsoft.Phone.Reactive dll.

    But Microsoft.Phone.Reactive is not present in windows phone 8.1. And moreover we cannot use prism in our application.

    Can somebody please help us to figure out how can we implement the event aggregator (subscribe/unsubscribe) for events in our windows phone 8.1 application?

    Any pointers would help us here.

    Monday, November 10, 2014 7:17 AM

Answers

  • can't get an existing one like:

    https://www.nuget.org/packages/Prism.PubSubEvents/1.1.2


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, November 10, 2014 8:46 AM
  • they splitted up prism so you can just use their event aggregator now without getting all the other things in

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, November 10, 2014 9:23 AM
  • >>Without using prism, is there any other way of implementing it?

    Yes, of course there is (after all Microsoft did), but why go through all this trouble when there is one available in a commonly used framework from Microsoft?

    I hope you don't expect anyone here to write an entire event aggregator from scratch for you. The forums are for helping solving specific issues and provide guidelines, not for anyone to write an entire class or application for you.

    I would just install the Prism.PubSubEvents package using Nuget: https://www.nuget.org/packages/Prism.PubSubEvents/

    ..and add an ApplicationService class to the application:

    using Microsoft.Practices.Prism.PubSubEvents;
    
    namespace App1
    {
      internal sealed class ApplicationService
      {
        private ApplicationService() {
        }
    
        private static readonly ApplicationService _instance = new ApplicationService();
    
        internal static ApplicationService Instance {
          get {
            return _instance;
          }
        }
    
        private IEventAggregator _eventAggregator;
        internal IEventAggregator EventAggregator {
          get {
            if (_eventAggregator == null)
              _eventAggregator = new EventAggregator();
    
            return _eventAggregator;
          }
        }
      }
    }
    

    You can then use the EventAggregator like this from any component in your app (without using any other stuff that is included in Prism):

    //publish:
    var eventAggregator = ApplicationService.Instance.EventAggregator;
    eventAggregator.GetEvent<YourEvent>().Publish("arg");
    
    //subscribe:
    var eventAggregator = ApplicationService.Instance.EventAggregator;
    eventAggregator.GetEvent<YourEvent>()
                .Subscribe((arg) => { ... });
    

    Hope that helps.

    Please remember to mark helpful posts as answer and/or helpful.

    Monday, November 10, 2014 11:23 AM

All replies

  • can't get an existing one like:

    https://www.nuget.org/packages/Prism.PubSubEvents/1.1.2


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, November 10, 2014 8:46 AM
  • Thanks for your reply.

    Without using prism, is there any other way of implementing it?

    • Proposed as answer by Dave SmitsMVP Monday, November 10, 2014 9:22 AM
    • Unproposed as answer by Dave SmitsMVP Monday, November 10, 2014 9:22 AM
    Monday, November 10, 2014 9:12 AM
  • they splitted up prism so you can just use their event aggregator now without getting all the other things in

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, November 10, 2014 9:23 AM
  • >>Without using prism, is there any other way of implementing it?

    Yes, of course there is (after all Microsoft did), but why go through all this trouble when there is one available in a commonly used framework from Microsoft?

    I hope you don't expect anyone here to write an entire event aggregator from scratch for you. The forums are for helping solving specific issues and provide guidelines, not for anyone to write an entire class or application for you.

    I would just install the Prism.PubSubEvents package using Nuget: https://www.nuget.org/packages/Prism.PubSubEvents/

    ..and add an ApplicationService class to the application:

    using Microsoft.Practices.Prism.PubSubEvents;
    
    namespace App1
    {
      internal sealed class ApplicationService
      {
        private ApplicationService() {
        }
    
        private static readonly ApplicationService _instance = new ApplicationService();
    
        internal static ApplicationService Instance {
          get {
            return _instance;
          }
        }
    
        private IEventAggregator _eventAggregator;
        internal IEventAggregator EventAggregator {
          get {
            if (_eventAggregator == null)
              _eventAggregator = new EventAggregator();
    
            return _eventAggregator;
          }
        }
      }
    }
    

    You can then use the EventAggregator like this from any component in your app (without using any other stuff that is included in Prism):

    //publish:
    var eventAggregator = ApplicationService.Instance.EventAggregator;
    eventAggregator.GetEvent<YourEvent>().Publish("arg");
    
    //subscribe:
    var eventAggregator = ApplicationService.Instance.EventAggregator;
    eventAggregator.GetEvent<YourEvent>()
                .Subscribe((arg) => { ... });
    

    Hope that helps.

    Please remember to mark helpful posts as answer and/or helpful.

    Monday, November 10, 2014 11:23 AM