Andrei Popescu
Andrei Popescu

Reputation: 65

Using the IEventAggregator, is it possible to first Publish an event and then Subscribe to it?

Maybe my method of implementing this was wrong, but here my out:

I'm currently developing an app using MVVM and Prism, with a login dialog and multiple modules:

  1. the MainWindowViewModel subscribes to an event: _ea.GetEvent<RegisterMenuItemEvent>().Subscribe(AddMenuItem, true); that checks if has to add a module name to a listview;
  2. on startup it dynamically loads all modules from a folder:
    • each IModule class listens for a LoginEvent: _ea.GetEvent<LoginEvent>().Subscribe(CheckRoles, true); that checks if the user has the permission to navigate to module's view;
    • if it complies, then, withing the CheckRoles() method it publishes an event that registers the module as a listview item: _ea.GetEvent<RegisterMenuItemEvent>().Publish(new MenuItem(null, View, ModuleName, GroupName));
  3. after you log in
    • it registers the modules as listview menu items, based on your given role for each module: _ea.GetEvent<LoginEvent>().Publish(new LoginInfo(ctxUser.Username, ctx.UserRole.Where(r => r.UserId == ctxUser.Id).Select(r => r.Role.Name).ToList()));
    • sets up the authenticated username to MainWindowView

Everything above, is executed in the exact order.

My question is, how can I subscribe to an event that has been already published?:

Upvotes: 0

Views: 191

Answers (1)

mm8
mm8

Reputation: 169150

My question is, how can I subscribe to an event that has been already published?

You can't. Well, you can indeed subscribe to the event but you won't get any already published events if you see what I mean.

So you need to make sure that you subscribe to the event before any other component publishes the event if you want to be sure not to miss any events.

Upvotes: 2

Related Questions