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

In the first part of this series of articles about Prism, we started by showing the first steps, learning how to install it, register pages and services, basic navigation between pages, passing parameters, etc.

In this article, we will continue talking about some more advanced topics on Navigation and also will show some handy stuff like Delegate Commands, Bindable Base, Platform services, etc.

Let’s start with Navigation

NavigationPage

In Prism to use a Xamarin Forms NavigationPage is really easy,  you can do it in two simple steps:

1-Register the NavigationPage 

Update: using Prism Version 7.0 > 


2-Add the “NavigationPage” to the uri when navigating

Also, if you want to create your own navigation page because you might want to change the navigation bar color or for whatever other reason, just need to:

1-Create a new class and inherit from NavigationPage

2-Register your custom navigation page class

Update: using Prism Version 7.0 >

 

3-Instead of using NavigationPage when doing the Navigation, use the name of your custom navigation page class

Deep Linking

One of the things I love the most about prism is “Deep linking”  which allows creating your own navigation stack of pages in a very easy and intuitive way.

For example, imagine you have the following pages in your project:

 

And you want to go to the last page of the ones shown above (Home Detail Option Page), but also you want to have all the pages added into your navigation stack before this page so if you press back you will see the page before that one.

  1. Goes to HomeDetailOptionPage
  2. Pressing back takes you to HomeDetailPage
  3. Pressing Back takes you to HomePage
  4. Pressing Back takes you to LoginPage

For doing that you can do something like this:

Can Navigate

CanNavigate is a method provided by Prism, in which one you set if you can navigate or not.  To use it is simple, you just have to implement the interface called IConfirmNavigation and use the method CanNavigate.

If the method is set to false, you won’t be able to navigate to any page even if you do _navigationService.NavigateAsync(….).

Bindable Base

Prism provides this class to help you with the property changes. Basically, if you inherit from this class, you don’t have to call the RaisePropertyChanged method in each property, the method SetProperty will take care of this internally for you.

Platform Services

Normally in Xamarin Forms when we create a service to access a platform specific feature we define a dependency service and we have to do a static call using DependencyService.Get<> for this Prism has a better and simpler approach.

Let’s do it step by step using as an example a service to get the battery status:

1-Create an interface for your for service on your shared/pcl project.


2- Create your service class and provide the interface implementation on each platform 

iOS:


3-Register your service class implementation in each platform

In the first part of this article series, when we started creating the project, we defined a class to specify registration for services per platform, basically in that class is where we are going to register all the platform services to be used.

Update: Using Prism Version 7.0 >

4-Inject your service

Instead to call DependencyService.Get<IBatteryService>, we will inject the BatteryService by using IBatteryService interface. Now we can use the methods defined on it.

Is important to mention that the steps 3 and 4 apply for plugins too. For example, if we want to use the Plugin Acr.UserDialogs, we just have to install the plugin NuGet package in all the projects and then register it. We can use by injecting it as shown in step 4.

Update: Using Prism Version 7.0 >

containerRegistry.RegisterInstance<IUserDilalog>(UserDialog.Instance);

Delegate commands

Imagine you have an use case in which you want to validate if you can execute an action or not. Normally in Xamarin Forms using an ICommand you would do something like this:

For those scenarios Prism provides the DelegateCommands basically, they are commands that allow you to have an observable CanExecute state, this means the property/method associated to it will always be observed updating CanExecute state automatically.

So using Delegate Commands with an observable CanExecute will be something like this:

Some advantages of using it are:

  • The CanExecute state is always being observed, so you don’t have to worry about the property/method status change to provide the correct command state.
  • It automatically updates the IsEnable property of the control attached, so if the property is false you won’t be able to execute any action of that control. If then it’s changed to true will automatically update the CanExecute state.

As you can see on the image above, the button is not enabled, because I’m binding the command property to the GetBatteryStatusCommand of my ViewModel, and the CanExecute state is provided by AllFieldsAreValid property which is set to false.

IDestructible 

Using Xamarin Forms sometimes we have the necessity of cleaning something when the Page/ViewModel is destroyed.  For example: unsubscribing to events, remove event handlers, etc.

For that Prism provides the IDestructible interface that contains the Destroy() method which is called when the Page/ViewModel is destroyed. You just have to implement the interface “IDestructible” and now you will have the method “Destroy” method available for any cleanup you might need.

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

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

Sample Updated with Prism v7 > :

https://github.com/CrossGeeks/PrismV7Sample

Here are other good references about Prism:

Happy coding and see you in the next Prism article.

You may also like

3 Comments

  1. This is one of the best Xam Prism posts I’ve seen regarding navigation. Thanks for posting

  2. I read in other post of yours about Fody and INotifyPropertyChanged via attributes. Pretty cool.

    How does this integrate with the Prism “SetProperty” and the handling of property changes in the BindableBase class? Does it duplicate? Do we pick one or the other?

    If so, which do you prefer?

    As always, thanks!

    1. You can mix both, if you create a new project using the template prism pack you will see they use both, but Personally I prefer to use INotifyPropertyChange instead of BindableBase, why? Because I like to not specify the SetProperty to keep the code clean.