Updating to Prism 7.0 for Xamarin Forms

Recently Prism for Xamarin Forms has released the version 7.0, which is a great one with lots of improvements. If you have an older version you will see there’s a lot of breaking changes when trying to update your project.

In this article, I’m going to guide you step by step by applying the changes needed for this version and showing some of the new features.

Note: This article isn’t about learning Prism, if you want to learn about it you can check this.

So let’s start

Before we start is important to know about some general changes:

    • .Net Standard Support
    • Remove Container Dependencies:  In the previous version of Prism, the container handler was done by using the <container you installed> API, but now is done by interacting with the IContainerProvider and IContainerRegistry (those are part of Prism itself). So now for example when registering a new View instead of doing something like this Microsoft.Practices.Unity.Container.RegisterTypeForNavigation you will do Prism.Ioc.IContainerRegistry.RegisterForNavigation.  

Let’s update 

For the purpose of this article, I will clone this sample , which is using the version 6.3.0.

After clicking on update on all the Prism packages in your projects probably you will see that the project doesn’t compile and it will have a lot of errors.

Let’s fix them:

General

App.cs

  • Solve the IPlatformInitializer missing reference
    • Change the Microsoft.Practices.Unity to the Prism Reference (using Prism)
  • Solve the RegisterTypes()
    • The method RegisterTypes() changed, now it receive the IContainerRegistry: RegisterTypes(IContainerRegistry containerRegistry)
    • As I mentioned above instead of Container.RegisterTypeForNavigation<CustomTabbedPage>(); you will replace it for containerRegistry.RegisterForNavigation<CustomTabbedPage>();  (as you can see the RegisterTypeForNavigation changed to RegisterForNavigation).

Going per platform

iOS (AppDelegate.cs)

  • Solve the IPlatformInitializer missing reference
    • Change the Microsoft.Practices.Unity to the Prism Reference (using Prism)
  • Solve the RegisterTypes()
    • The method RegisterTypes() changed, now it has the IContainerRegistry as parameter: RegisterTypes(IContainerRegistry containerRegistry)
    • Change containerRegistry.RegisterInstance<IBatteryService>(batteryService, new Unity.Lifetime.ExternallyControlledLifetimeManager()); to  containerRegistry.RegisterInstance<IBatteryService>(batteryService);

Also do the same for Android (MainActivity.cs).

TabbedPage

If you are using TabbedPage in your project and you want to select a tab, probably you are doing something like this:

Now you will change it for:


As you can see now is using the parameter selectedTab to indicate it.

New feature: 

  • Dynamic Tab Generation

Now is possible to create tabs dynamically. So for example, if we have this XAML:

But we want to add a new tab at runtime, we use the parameter createTab to do this:

Also if you want that the newly created tab to have a NavigationPage we can do it by adding the NavigationPage + “|” + “your page”:

Modules

If you are using modules, there are a few changes:

App.cs 

  • The ConfigureModuleCatalog now receives the IModuleCatalog
  • Use the moduleCatalog received as a parameter instead of using the ModuleCatalog class
  • Send the module type by parameter when creating ModuleInfo instance

Main module class

  • Now you will register your pages and services in the RegisterType method, also it will receive the IContainerRegistry

Navigation

Remove Pages from Navigation Stack

Now we can remove pages when navigating to another page.

So, for example in the image above we want to remove the LoginPage when navigating to the HomePage.

The code will be:

As you can see we are replacing the page with  ../ (“LoginPage/HomePage), you can remove as many pages as you want from the navigation stack using this feature.

GoBackToRoot support

Now we can use GoBackToRootAsync which does the same as Xamarin Forms PopToRoot method, basically removes all the pages from navigation stack except for the root.

 

You can see the full source code updated here:

https://github.com/CrossGeeks/PrismV7Sample

That’s all for now, for more information about the updates you can read this article. Also special thanks to Brian Lagunas, Dan Siegel and all the Prism contributors, who worked so hard to release this awesome version :). 

Happy coding!

You may also like

1 Comment