发布的事件未被订阅或发布
Published Event not being subscribed or Published
当我尝试将事件从 SimpleFreeDrawModifier class 发布到 FieldSettingViewModel class 时,订阅者 class 无法订阅事件,反之亦然。
发布活动代码:
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();
}
}
}
}
订阅活动代码
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();
}
请问有什么建议吗?
如果我错了请纠正我,但看起来 SimpleFreeDrawModifier
和 FieldSettingViewModel
的实例化是通过依赖注入实现的。
您确定 FieldSettingViewModel
的实例已经创建并且它正在接收相同的 IEventAggregator
实例吗?
这个没有 DI 的最小示例有效
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
{
}
检查您的 EventAggregator 的 DI 生命周期是 Singleton
而不是 Transient
,也尝试在 FieldSettingViewModel
的构造函数中放置一个断点以确保它是 运行。
如果它不是 运行 并且您要像 services.AddSingleton<FieldSettingViewModel>()
一样为 DI 注册它,请尝试使用 services.AddSingleton<FieldSettingViewModel>(new FieldSettingViewModel())
来确保实例化。
SimpleFreeDrawModifier 的构造函数使用 EventAggregator 而不是接口 IEventAggregator 因此由hwoodiwiss 他们没有使用相同的实例,即使它已被注册为单例。
它已经发生在我身上,我花了很长时间才看到失踪的我
当我尝试将事件从 SimpleFreeDrawModifier class 发布到 FieldSettingViewModel class 时,订阅者 class 无法订阅事件,反之亦然。
发布活动代码:
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();
}
}
}
}
订阅活动代码
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();
}
请问有什么建议吗?
如果我错了请纠正我,但看起来 SimpleFreeDrawModifier
和 FieldSettingViewModel
的实例化是通过依赖注入实现的。
您确定 FieldSettingViewModel
的实例已经创建并且它正在接收相同的 IEventAggregator
实例吗?
这个没有 DI 的最小示例有效
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
{
}
检查您的 EventAggregator 的 DI 生命周期是 Singleton
而不是 Transient
,也尝试在 FieldSettingViewModel
的构造函数中放置一个断点以确保它是 运行。
如果它不是 运行 并且您要像 services.AddSingleton<FieldSettingViewModel>()
一样为 DI 注册它,请尝试使用 services.AddSingleton<FieldSettingViewModel>(new FieldSettingViewModel())
来确保实例化。
SimpleFreeDrawModifier 的构造函数使用 EventAggregator 而不是接口 IEventAggregator 因此由hwoodiwiss 他们没有使用相同的实例,即使它已被注册为单例。
它已经发生在我身上,我花了很长时间才看到失踪的我