Prism, Event published in shell not caught in module! - wpf

I have a wpf composite application with 3 or 4 modules that will always be loaded. In my shell I have a toolbar with buttons to active corresponding views from these modules. I injected an IEventAggregator into the shell presentor so that when a button is pressed I can publish an event that the corresponding module controller has subscribed to. When the event is caught the controllor will active its view.
Thats the theory anyways, in practice my controller is not catching the event. There are no errors in publishing or subscribing. I thought at first that there might be an issue with the eventAggregator not being the same, but thats not the case, and the event has a subscriber when its published.
Can anyone think of a reason why the event is not getting caught?
(Or any suggestions on a different way to get my view to show would be helpful too!)

Do you have your module controller 'alive'? Are you subscribing to the event using weak delegate reference or strong reference?
Seems that what is happening is that your module controller is being disposed, and so, the event is not getting caught.
To subscribe with a strong reference, use the keepSubscriberReferenceAlive option on the Subscribe method.
You can check the Event Aggregator article from the documentation which might provide more insight.
If that still does not works, can you share your repro code with me so I can take a look to it? (ezequieljadib at hotmail dot com)
Thanks,
Ezequiel Jadib

Related

Can I get EventAggregator Subscribe Message without view, viewmodel in prism?

I'm trying to make a module in a prism project. The module doesn't have UI(View, ViewModel). I want the module has working EventAggregator event handlers.
Is there any way?
I wonder it is possible to make service module without view, viewmodels?
Of course, it is. A module is just a late-bound, post-deployment-exchangeable collection of code. It can contain anything, be it views, view models, services, dtos...
I want the module has working EventAggregator event handlers.
... most of those can have dependencies injected because they're resolved from the container. I.e. they can use the event aggregator.
If you have services that are "on their own" (i.e. like a windows service, but in-process) or that need to be initialized early, you can resolve the service in OnInitialized and start it/initialize it.

Tightly coupled controllers architecture

Getting started with Angular, I am trying to implement a toolbar.
The application is structured in a menu bar, a view and the toolbar that form the web-page.
This toolbar has a general purpose (provides help functionality and error display), but for specific views adds some buttons that control the functionality (save, cancel, edit, delete, etc).
In the current design the view and the toolbar are siblings. The view controller depends on the data the view contains, and it can have different functionality. (For example: A view might allow data import, so there will be an import function in the toolbar, while other view might not.)
My problem is I cannot picture the structure of the communication between the view and the toolbar. Because the controllers are tightly coupled a service does not seem to address the communication.
Any help?
You can:
Share data between different controllers/services using rootScope
(use wisely)
You can Publish and Subscribe for app events using
$on, $broadcast and $emit
One good strategy which I have used and helped is to keep all App events definition in one service called something like AppEvents so you can easily keep track of them and control what event causes what from a single place.
Here is one nice article to read that expands more on this topic

notifications/error handling in marionettejs

I'm building an application using Marionette, I'm trying to write come with an idea/pattern for handling notification on the application.
What I mean is when the user does some action we should show some notification(success/error), if an error occurs in the application we should show some notification.
I was wandering what is the best way/approach in handling some cases.
If you have some experience/link or any information will be helpful.
You should use events. You can see an example here:
https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_view.js : a view triggers an event (e.g. delete a model)
https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_controller.js : the controller listen for and processes that event. The same concept should be used for success/failure/error management.
There's an example of using events to manage routing in the free sample to my book on Marionette.

MVVM firing event back to view

I have a ViewModel where one of its functions is to talk to a service and get some data. However, if there is a problem, I would like a notify the user that the service could not run.
Currently what I am doing is firing an event which the view has subscribed to (my viewModel is created in the resources section of the view) and receiving the event in the view event handler I just do a Windows.Alert().
First, I am trying to reduce the amount of code in the code behind of the view and with the event firing, there must be a better way to do this?
Secondly, since my view knows about my view model (i.e. created in the resources section), I am sure this will cause problems in testing my view. Is this the correct way to do this?
JD.
Yes, I don't think subscribing to an event in the VM from the View is a good idea. Almost better to put the alert in the VM, but that puts UI in the VM and makes it hard to test. There are a couple of other ways to handle this.
Dialogs and ViewModel - Using Tasks as a Pattern
Decoupled ChildWindow Dialogs with Prism in Silverlight 3
Best to use a service here. A service just provides some function through an interface.
public interface IDialogService {
void ShowNotifictation(string message);
}
The ViewModel takes this service and uses it to display the notification. The implementation of this service is then specific to your solution, and can display the notification however you want.
The implementation of such a service might look like this:
[Export(typeof(IMessageService))]
public class MessageService : IMessageService
{
public void ShowMessage(string message)
{
MessageBox.Show(message);
}
...
It uses MEF as IoC Container. The service is registered via the Export attribute as IMessageService.
You might have a look at the WPF Application Framework (WAF) to see the full implementation and sample applications that are using this service.
Hope this helps.
jbe

How to modules in Prism (CAL) communicate with each other?

I've got a WPF application which uses the MVVM pattern throughout, no code-behind, the ViewModels communicate with each other through the MainViewModel which gets injected into each of them.
Eventually, this application needs to be incorporated into an application which uses Composite Application Library, Unity, etc. Looking through the code and documentation of CAL, I can see how I can register my whole application as a module in the CAL application, but how is my application-as-module going to communicate with the other modules that are also dynamically loaded? I'm expecting, e.g. that each module gets the CAL application somehow injected, or that there is some kind of Event Controller or Messenger with which I can loosely communicate with the other modules, i.e. can send a message and respond to events but not worry if those modules are actually there or not.
How do Composite Application modules communicate with each other?
If you are using CAL(Prism) look into the Event Aggregator/CompositePresentationEvent where it uses the Publisher/Subscriber pattern (aka Pub/Sub) so some Modules of the app is subscribed to an Event Aggregator, so when another Module has changes it will Publish changes e.g.(SelectedItemChanged) to the Event Aggregator, If Other Modules are interested in the changes Published they will act within there part of the application.
Example:
A Desktop e-mail Application:
Modules:
Mail Items (MailID,Subject,Sender,SentDate..etc)
Detail View (MessageBody)
If the selection in the Mail Items ListBox gets changed,It publishes the MailID to the Event Aggregator then Detail View knows about the change and then it grabs the MessageBody for that E-mail by MailID. where "MailItems" and "DetaliView" Modules have been developed by different teams but they have MailID as a common expected message in between.
Check out Prism's event aggregator.

Resources