In my Prism 6 WPF MVVM modular application (using Unity DI) I want to do communicating between modules using loosely coupled event - one module is publisher and the other modules are subscribers. On the side of publisher in AuthorizationViewModel class I have, in particular, the following methods:
public class AuthorizationViewModel : BindableBase
{
. . . . .
// This method is called from the command method when user clicks button in the view.
private void authenticateUser(string userName, string userPassword, Action<UserAuthorizationLevel> successCallback, Action<string> failureCallback)
{
Task task = Task.Run(() =>
this.getUsers((users) =>
{
// Get authenticated user information.
var userAuthenticated = GetUserByNameAndPassword(userName, userPassword, users);
// Call method publishing loosely coupled event if the user exists. Else display the error message.
if (userAuthenticated != null)
successCallback(userAuthenticated.AuthorizationLevel);
else
failureCallback("Authentification failed.");
}));
}
. . . . .
}
Below is successCalback definition that is in AuthorizationViewModel class too:
private void successCalback(UserAuthorizationLevel authorizationLevel)
{
// Publish loosely coupled event.
this._eventAggregator.GetEvent<UserAuthorizationLevelDeterminedEvent>().Publish(authorizationLevel);
}
UserAuthorizationLevel here is enum type defined in a common place of my application solution and I don't display it here. UserAuthorizationLevelDeterminedEvent is the event type that is also defined in a common place of my application solution. Below I display it:
public class UserAuthorizationLevelDeterminedEvent : PubSubEvent<UserAuthorizationLevel>
{
}
successCalback method runs whenever it is necessary and its line of code
this._eventAggregator.GetEvent<UserAuthorizationLevelDeterminedEvent>().Publish(authorizationLevel);
executes fine so event is published but the subscriber doesn't react to event at all! There is no response to event on subscriber side! Below I display code on subscriber side:
public class CalibrationNavigationItemViewModel : BindableBase
{
. . . . .
private IEventAggregator _eventAggregator;
. . . . .
// The constructor; creates instance of CalibrationNavigationItemViewModel.
public CalibrationNavigationItemViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
{
. . . . .
this._eventAggregator = eventAggregator;
this._eventAggregator.GetEvent<UserAuthorizationLevelDeterminedEvent>().Subscribe(this.setRadiobuttonVisualStatus, ThreadOption.BackgroundThread);
. . . . .
}
. . . . .
// Changes visual status of Radiobutton in the View.
private void setRadiobuttonVisualStatus(UserAuthorizationLevel userAuthorizationLevel)
{
if (userAuthorizationLevel == UserAuthorizationLevel.Manufacturer)
this.IsVisible = Visibility.Visible;
else
this.IsVisible = Visibility.Collapsed;
}
// Controls visual status of Radiobutton in the View; the Visibility property
// of Radiobutton in the View is bound to this property.
public Visibility IsVisible
{
get { return this._isVisible; }
set { this.SetProperty(ref this._isVisible, value); }
}
}
(I bag your pardon I'll make a bad break here: I'm controlling visibility status of Radiobutton in module instead of loading module itself dynamically because my application must give an opportunity to change users within the same one session. Prism module can't be unloaded after its initializing.) Now, returning to our sheep; I set ThreadOption.BackgroundThread on the subscriber side because the publisher publishes the event in TPL Task but not in the UI thread. I'd like to know: Why does subscriber not react to published event at all? What I'm doing wrong? Please help me.