C# 使用Unity和prism EventAggregator将数据从一个VM传递到另一个VM

C# 使用Unity和prism EventAggregator将数据从一个VM传递到另一个VM,c#,wpf,mvvm,dependency-injection,prism,C#,Wpf,Mvvm,Dependency Injection,Prism,试图使用prism EventAggregator将数据从一个ViewModel传递到另一个ViewModel,但在订阅服务器上调试时,数据为null 使用prism的5.0版 更新 好的,我已经尝试使用prism 5.0版本实现EventAggregator。它仍然不起作用,但这是我所做的 1:创建事件类 public class RoomsSelectedEvent : PubSubEvent<ObservableCollection<Room>> { } 公共

试图使用prism EventAggregator将数据从一个ViewModel传递到另一个ViewModel,但在订阅服务器上调试时,数据为null

使用prism的5.0版

更新

好的,我已经尝试使用prism 5.0版本实现EventAggregator。它仍然不起作用,但这是我所做的

1:创建事件类

public class RoomsSelectedEvent : PubSubEvent<ObservableCollection<Room>>
{

} 
公共教室SelectedEvent:PubSubEvent
{
} 
2:在publisher ViewModel(BookingViewModel)上插入IEventagegrogator

公共类BookingViewModel:INotifyPropertyChanged,IViewBookingViewModel
{
//聚合器
受保护的只读IEventagegrator\u事件聚合器;
//命令
公共ICommand ContinueCommand{get;set;}
公共可观察收集室列表{get;private set;}
公共可观察收集下拉列表室{get;private set;}
公共ObservableCollection下拉列表客户{get;private set;}
//恩蒂斯
私有只读IDialogService_dialogServiceContactView;
私人只读igetrooms服务(getrooms服务);;
公共预订视图模型(IDialogService对话框ServiceContactView、IGetRoomsService GetRoomsService、IEventAggregator事件聚合器)
{
//注射
_dialogServiceContactView=dialogServiceContactView;
_getRoomsService=getRoomsService;
_eventAggregator=eventAggregator;
//命令
ContinueCommand=newrelayCommand(ContinueCommand\u DoWork,()=>true);
}
//继续命令
公共无效连续命令(对象对象对象)
{
ObservableCollection RoomsSelected=新的ObservableCollection();
RoomsSelected=\u getRoomsService.FilterSelectedRooms(RoomsList);
//发布事件:
_eventAggregator.GetEvent().Publish(已选择房间);
//打开新对话框
_dialogServiceContactView.ShowDialog();
}
}
3:在订阅者视图模型(ContactViewModel)中插入IEventaggGator

公共类ContactViewModel:IViewContactViewModel,INotifyPropertyChanged
{
//聚合器
受保护的只读IEventagegrator\u事件聚合器;
//性质
公共ObservableCollection SelectedRooms{get;set;}
公共联系人视图模型(IEventAggregator事件聚合器)
{
//注射
_eventAggregator=eventAggregator;
//订阅活动
_eventAggregator.GetEvent()
.Subscribe((数据)=>{SelectedRooms=data;});
}
公共收集室
{
获取{return SelectedRooms;}
设置{SelectedRooms=value;NotifyPropertyChanged();}
}
}
我读过这篇文章,可能是因为IEventAggregator在两种视图模型中都不一样。我使用Unity将其注入如下代码:

protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        //view & viewModels
        _container = new UnityContainer();
        _container.RegisterType<IViewMainWindowViewModel, MainWindow>();
        _container.RegisterType<IViewMainWindowViewModel, MenuViewModel>();
        _container.RegisterType<IViewBookingViewModel, BookingView>();
        _container.RegisterType<IViewBookingViewModel, BookingViewModel>();
        _container.RegisterType<IViewContactViewModel, ContactDetailsView>();
        _container.RegisterType<IViewContactViewModel, ContactViewModel>();
        _container.RegisterType<IGetRoomsService, GetRoomsService>();
        _container.RegisterType<IPostReservationService, PostReservationService>();
        _container.RegisterType<IGetReservationsListService, GetReservationsListService>();

        //types
         _container.RegisterType<IEventAggregator, EventAggregator>(new ContainerControlledLifetimeManager());
        _container.RegisterType(typeof(IDialogService<>), typeof(DialogService<>));

        _container.Resolve<MainWindow>().Show();
    }
启动时受保护的覆盖无效(StartupEventArgs e)
{
基础。启动时(e);
//视图和视图模型
_容器=新的UnityContainer();
_container.RegisterType();
_container.RegisterType();
_container.RegisterType();
_container.RegisterType();
_container.RegisterType();
_container.RegisterType();
_container.RegisterType();
_container.RegisterType();
_container.RegisterType();
//类型
_RegisterType(新的ContainerControlledLifetimeManager());
_RegisterType(typeof(IDialogService),typeof(DialogService));
_container.Resolve().Show();
}
发现我需要添加ContainerControlledLifetimeManager,但它仍然无法工作

在subscriber viewModel中调试时,我可以看到实例中有一个事件,如图所示:

它抓不住它,这就是问题所在:(

今天我尝试删除我安装的所有软件包(prism 5.0)并使用prism.Core(6.1版本)


这导致了相同的结果,当我调试时,我可以看到事件已发布,但当我订阅时,它仍然为空。

EventAggregator
UnityBootTrapper
基类自动注册,您无需自己注册。另外,两个视图模型必须同时处于活动状态。查看您的代码,你真的想使用引导程序,而不是自己实现所有的东西。
public class ContactViewModel : IViewContactViewModel, INotifyPropertyChanged
{
    //aggregator 
    protected readonly IEventAggregator _eventAggregator;

    //properties
    public ObservableCollection<Room> SelectedRooms { get; set; }

    public ContactViewModel(IEventAggregator eventAggregator)
    {
        //Injection
        _eventAggregator = eventAggregator;

        //Subscripe to event
        _eventAggregator.GetEvent<RoomsSelectedEvent>()
        .Subscribe((data) => { SelectedRooms = data; });
    }

    public ObservableCollection<Room> Rooms
    {
        get { return SelectedRooms; }
        set { SelectedRooms = value; NotifyPropertyChanged(); }
    }

}
protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        //view & viewModels
        _container = new UnityContainer();
        _container.RegisterType<IViewMainWindowViewModel, MainWindow>();
        _container.RegisterType<IViewMainWindowViewModel, MenuViewModel>();
        _container.RegisterType<IViewBookingViewModel, BookingView>();
        _container.RegisterType<IViewBookingViewModel, BookingViewModel>();
        _container.RegisterType<IViewContactViewModel, ContactDetailsView>();
        _container.RegisterType<IViewContactViewModel, ContactViewModel>();
        _container.RegisterType<IGetRoomsService, GetRoomsService>();
        _container.RegisterType<IPostReservationService, PostReservationService>();
        _container.RegisterType<IGetReservationsListService, GetReservationsListService>();

        //types
         _container.RegisterType<IEventAggregator, EventAggregator>(new ContainerControlledLifetimeManager());
        _container.RegisterType(typeof(IDialogService<>), typeof(DialogService<>));

        _container.Resolve<MainWindow>().Show();
    }