Search code examples
c#wpfmvvmprism

Published Event not being subscribed or Published


When I am trying to publish an events from SimpleFreeDrawModifier class to FieldSettingViewModel class, Subscriber class not able to get subscribe the events or vice versa.

Publishing Events Code:

  public SimpleFreeDrawModifier(RegionManager regionManager, EventAggregator eventAggragator)  // Constructor 
    {
        _regionManager = regionManager;
        _eventAggragator = eventAggragator;

        CorodinatesMapDictionary =new Dictionary<object, object>();
    }
     private void AppendPoint(Point mousePoint)
    {
        // On Mouse-down, append another point
        if (_dataSeries != null)
        {
             CorodinatesMapDictionary.Clear();
            _dataSeries.Append((double)XAxis.GetDataValue(mousePoint.X), (double)YAxis.GetDataValue(mousePoint.Y));
           
            GlobalVar.XCoordinate=(double)_dataSeries.XValues[0];
            GlobalVar.YCoordinate=(double)_dataSeries.YValues[0];

            CorodinatesMapDictionary.Add("X",  GlobalVar.XCoordinate);
            CorodinatesMapDictionary.Add("Y", GlobalVar.YCoordinate);

            var calc = Math.Abs(GlobalVar.XCoordinate) * 1.0913;
            if (GlobalVar.YCoordinate < 0)
            {
                if (GlobalVar.YCoordinate >= -(calc))
                {
                    _eventAggragator.GetEvent<MapCoOrdinateChangedEvents>().Publish();
                }
            }
            else
            {
                if(Math.Abs(GlobalVar.XCoordinate) <=25 && GlobalVar.YCoordinate <=25)
                {
                    _eventAggragator.GetEvent<MapCoOrdinateChangedEvents>().Publish();
                }
            }
        }
    }

Subscribing Events Code

  public FieldSettingViewModel(RegionManager regionManager,IEventAggregator eventAggragator)
    {
        _regionManager = regionManager;
        _eventAggragator = eventAggragator;

        ClearCommand = new DelegateCommand(Clear);
        ApplyFieldSettingCommand = new DelegateCommand(ApplyFieldSetting, canApplyFieldSetting);

        ScanAreaDataSeries = new XyDataSeries<double, double>();
      
        GetScanSeriesData();

        _eventAggragator.GetEvent<MapCoOrdinateChangedEvents>().Subscribe(MapValue);
    }
     private void MapValue()
    {
        //throw new NotImplementedException();
    }

Could you please provide any suggestions?


Solution

  • Correct me if I'm wrong but it looks like instantiation of SimpleFreeDrawModifier and FieldSettingViewModel is achieved through dependency injection.

    Are you sure that an instance of FieldSettingViewModel has been created and the it's receiving the same instance of IEventAggregator?

    This minimal example without DI works

    using System.Drawing;
    using Prism.Events;
    
    var eventAggregator = new EventAggregator();
    
    var pub = new Publisher(eventAggregator);
    var sub = new Subscriber(eventAggregator);
    
    pub.AppendPoint(new Point());
    
    
    public class Publisher
    {
        private IEventAggregator _eventAggregator;
    
        public Publisher(IEventAggregator eventAggregator)  // Constructor 
        {
            _eventAggregator = eventAggregator;
    
        }
        public void AppendPoint(Point mousePoint)
        {
    
            _eventAggregator.GetEvent<MapCoOrdinateChangedEvents>().Publish();
        }
    }
    
    public class Subscriber
    {
        private IEventAggregator _eventAggregator;
    
        public Subscriber(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
    
            _eventAggregator.GetEvent<MapCoOrdinateChangedEvents>().Subscribe(MapValue);
        }
    
        private void MapValue()
        {
            Console.WriteLine("Map value called");
        }
    }
    
    
    internal class MapCoOrdinateChangedEvents : PubSubEvent
    {
    }
    

    Check the DI lifetime of your EventAggregator is Singleton and not Transient, also try putting a breakpoint in the constructor of FieldSettingViewModel to ensure it is run.

    If it's not being run and you're registering it for DI like services.AddSingleton<FieldSettingViewModel>(), try instead using services.AddSingleton<FieldSettingViewModel>(new FieldSettingViewModel()) to ensure there is one instantiated.