Reputation: 65
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:
MainWindowViewModel
subscribes to an event: _ea.GetEvent<RegisterMenuItemEvent>().Subscribe(AddMenuItem, true);
that checks if has to add a module name to a listview;_ea.GetEvent<LoginEvent>().Subscribe(CheckRoles, true);
that checks if the user has the permission to navigate to module's view;CheckRoles()
method it publishes an event that registers the module as a listview item: _ea.GetEvent<RegisterMenuItemEvent>().Publish(new MenuItem(null, View, ModuleName, GroupName));
_ea.GetEvent<LoginEvent>().Publish(new LoginInfo(ctxUser.Username, ctx.UserRole.Where(r => r.UserId == ctxUser.Id).Select(r => r.Role.Name).ToList()));
MainWindowView
Everything above, is executed in the exact order.
My question is, how can I subscribe to an event that has been already published?:
_ea.GetEvent<MessageEvent>().Publish("Username");
_ea.GetEvent<MessageEvent>().Subscribe(GetUsername);
, but it doesn't work.Upvotes: 0
Views: 191
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