Prism - 已发布的事件未被订阅
Prism - Published Event not being subscribed
我在 WPF 中编写代码,其中有两个视图模型。在一个视图模型中,我正在关闭一个棱镜弹出窗口 window 并在关闭时调用我的发布方法,如:
private void OnCancelInteraction()
{
_eventAggregator.GetEvent<PubSubEvent>().Publish();
this.FinishInteraction();
}
而我的订阅事件是这样的:
public ResidentListViewModel(IResidentService residentService, IUserService userService, IEventAggregator eventAggregator)
{
var res = eventAggregator.GetEvent<PubSubEvent>().Subscribe(InitializeCollectionView);
_residentService = residentService;
_userService = userService;
_eventAggregator = eventAggregator;
InitializeCollectionView();
//***The InteractionRequest class
FormDialogOpenRequest = new InteractionRequest<INotification>();
ConfirmationRequest = new InteractionRequest<IConfirmation>();
//*** Commands for each of the buttons
RaiseFormDialogOpenCommand = new DelegateCommand<object>(this.RaiseFormDialogOpen);
aiseConfirmationCommand = new DelegateCommand<object>(this.RaiseConfirmation);
}
以下是这两个 classes 接收事件聚合器的方式:
UnityContainer container = new UnityContainer();
var aggregator = container.Resolve<EventAggregator>();
this.DataContext = new ResidentFormDialogViewModel(residentService, userService, unitService, aggregator);
这是我的第二个 class:
UnityContainer container = new UnityContainer();
var aggregator = container.Resolve<EventAggregator>();
DataContext = new ResidentListViewModel(residentService, userService, aggregator);
每次我取消互动时,发布都会发布,但不会调用 ResidentListViewModel
进行订阅。
ResidentListViewModel
在另一个具有订阅方法的 class 之前被调用。这会是个问题吗?我只想在交互完成时初始化我的集合视图而不破坏 MVVM 模式?如果还有其他方法请提出。
确保将 _eventAggregator 对象配置为 Singleton 并在两个视图模型中使用此单个实例。您遇到的问题可能与用于发布和订阅事件的不同 EventAggregator 对象有关。
我在 WPF 中编写代码,其中有两个视图模型。在一个视图模型中,我正在关闭一个棱镜弹出窗口 window 并在关闭时调用我的发布方法,如:
private void OnCancelInteraction()
{
_eventAggregator.GetEvent<PubSubEvent>().Publish();
this.FinishInteraction();
}
而我的订阅事件是这样的:
public ResidentListViewModel(IResidentService residentService, IUserService userService, IEventAggregator eventAggregator)
{
var res = eventAggregator.GetEvent<PubSubEvent>().Subscribe(InitializeCollectionView);
_residentService = residentService;
_userService = userService;
_eventAggregator = eventAggregator;
InitializeCollectionView();
//***The InteractionRequest class
FormDialogOpenRequest = new InteractionRequest<INotification>();
ConfirmationRequest = new InteractionRequest<IConfirmation>();
//*** Commands for each of the buttons
RaiseFormDialogOpenCommand = new DelegateCommand<object>(this.RaiseFormDialogOpen);
aiseConfirmationCommand = new DelegateCommand<object>(this.RaiseConfirmation);
}
以下是这两个 classes 接收事件聚合器的方式:
UnityContainer container = new UnityContainer();
var aggregator = container.Resolve<EventAggregator>();
this.DataContext = new ResidentFormDialogViewModel(residentService, userService, unitService, aggregator);
这是我的第二个 class:
UnityContainer container = new UnityContainer();
var aggregator = container.Resolve<EventAggregator>();
DataContext = new ResidentListViewModel(residentService, userService, aggregator);
每次我取消互动时,发布都会发布,但不会调用 ResidentListViewModel
进行订阅。
ResidentListViewModel
在另一个具有订阅方法的 class 之前被调用。这会是个问题吗?我只想在交互完成时初始化我的集合视图而不破坏 MVVM 模式?如果还有其他方法请提出。
确保将 _eventAggregator 对象配置为 Singleton 并在两个视图模型中使用此单个实例。您遇到的问题可能与用于发布和订阅事件的不同 EventAggregator 对象有关。