Prism in Xamarin Forms Step by Step (Part. 3)

In the second part of this series about Prism, we talked about Deep linking, Delegate Commands, Platform services, etc.

In this article, we will learn about more interesting concepts: Modules, Event Aggregator and EventToCommand.

Event Aggregator

Let’s say you have an application in which there’s a specific part of your code where you want to listen to an action that can occur anywhere within your application. A good option for handling this is by using Event Aggregator which uses a publish/subscribe event mechanism, that allows components to communicate in a loosely coupled way.

  Picture from Magnus Montin blog 

The way it works is that you have events that can have multiple subscribers (Where you want to receive the action) and publishers (From where you will send the events).

Let’s do it step by step:

In this example, we are going to update the Navigation bar color dynamically according to if the LoginPage is displayed or not.

 

1- Create a class for your event that inherits from the PubSubEvent class, passing as a parameter the type the data you want to return (in my case is a bool).

2- In the class you want to listen to the event inject the IEventAggregator by using dependency injection. On my case will be on CustomNavigationPage.

3- In the class you want to listen to the event, subscribe to the event using the method GetEvent with our event class UpdateNavBarEvent as the generic type finally we call the Subscribe method and pass our parameter.

Once you finish using your event, make sure to unsubscribe. If you don’t do it and subscribe again you will be receiving the event twice.

4- Inject the event aggregator in the class you will send the event (HomePage in my case) by using Publish method to send the event.

Event to Commands

One nice feature of Prism is the EventToCommandBehavior, which is a behavior that allows you to convert events into Commands. Which is really helpful when you have controls that only provide event handlers and you don’t want to use the code behind to handle it instead you would like to handle it on the ViewModel.

Let’s do it step by step:

In this example, we are going to use it to convert the “Completed” event of the Entry in a command.

1: Add the Prism Behavior reference in your XAML

2-Add the EventToCommandBehavior and in the property EventName specify the name of the event you want to convert, in my case, I’m using “Completed” because I want it to raise the OnLoginCommand when the user presses the return button.

 

Modules

Imagine a use case in which you have a company with multiple applications and all use the same authentication UI and logic (Login/Register). For this use case, you can use modules it allows you to separate an specific part of your application and reuse it.

 

Let’s do it step by step: 

1-The first thing we are going to do is creating a folder in which we will have all our modules (This optional step, but I like to do it, to have well-organized solution)

2-Do right click in your Modules folder and create a new project

3-Install the Prism Package in your Authentication Module

4-Create a class that will be the starting point of your module, it should implement the IModule interface

5-On the Initialize method register all the pages you are going to use in your module. 

Update: using Prism Version 7.0 >

6-In your PCL project add the reference of your module

7-In the App.cs class of your PCL project add your module by overriding the method ConfigureModuleCatalog

Update: using Prism Version 7.0 >

Also, see that in the InitializeMode it has two options: OnDemand and WhenAvailable

Use OnDemand when you don’t want to load the module when the application starts and instead you will like to load it at a specific point.

If your module is OnDemand you have to use the LoadModule method from the ModuleManager to load it. In order to access to it you will have to inject the IModuleManager and then you can call the LoadModule method with the name of your module as the parameter.

If your module is WhenAvailable it will load when the application starts and you don’t have to call the LoadModule method.

8-The last step is to navigate to pages in your module. For example, if I want to go to the LoginPage that is in my module, I just have to navigate to it ( I don’t have to register that page in my PCL project because it is already registered in the module)

That’s all for now, you can see the full source code used for these examples here:

https://github.com/CrossGeeks/Xamarin.Samples/tree/master/Xamarin%20Forms/PrismUnitySample

Sample Updated with Prism v7 > :

https://github.com/CrossGeeks/PrismV7Sample

References:

https://blog.magnusmontin.net/2014/02/28/using-the-event-aggregator-pattern-to-communicate-between-view-models/

https://github.com/PrismLibrary/Prism/blob/master/docs/Xamarin-Forms/6-EventToCommandBehavior.md

http://prismlibrary.readthedocs.io/en/latest/WPF/04-Modules/

 

Happy coding and see you in the next Prism article.

You may also like

6 Comments

  1. I have to say that this is wonderful. I am so relieved that you are writing and sharing what you know. 2 Thumbs up. Question – will you be doing one with masterdetail with the same detail as this article I am commenting on? Would be nice to have a few blog posts from you on this. I have tried and it doesn’t seem to work quite like I thought it would. Thanks and God bless

  2. Prism simplifies a lot of work, too bad that i learned about it about 2 months ago.

    I think that a post about how to securely store values on the phone would be great.

  3. Really great work. This post is informative and easy to understand at the same time. Thanks now i’m sure what Mvvm Framework to use for my projects.