I really like the functionality the EventAggregator from Prism offers me to loosely couple my classes. But now I have a problem which is not covered by the EventAggregator.
So let us assume I publish the message "Test" and at the moment there is no subscriber of this message. Is it possible to postpone it until a subscriber exists?
Regards
This is easy enough to implement. Define a second message that will be published later and a class that contains the message and a timer:
public class OriginalMessage : CompositePresentationEvent<OriginalArgs> { }
public class MyDelayedMessage : CompositePresentationEvent<MyEventArgs> { }
public class DelayedMessage {
EventAggregator _bus;
OriginalMessage _msg;
DispatcherTimer _timer;
OriginalArgs _args;
public DelayedMessage(
EventAggregator bus,
OriginalMessage sourceMessage,
OriginalArgs args,
TimeSpan delay
) {
_bus = bus;
_args = args;
_msg = sourceMessage;
_timer = new DispatcherTimer();
_timer.Interval = delay;
_timer.Tick += OnTimerTick;
}
void OnTimerTick(object sender, EventArgs args) {
_bus.GetEvent<MyDelayedMessage>().Publish(_args);
}
}
You'll need a context to place this object to keep it around. You can drop it onto the host IU container as a private field.